在Asp.Net MVC中设置“主页”

108

在 asp.net MVC 中,“主页”(即在访问 www.foo.com 时显示的路由)被设置为 Home/Index。

  • 这个值存储在哪里?
  • 如何更改“主页”?
  • 有没有比在主页控制器的 Index 操作中使用 RedirectToRoute() 更优雅的方法?

我尝试在项目中使用 grep 查找 Home/Index,但没有找到引用,也没有在 IIS(6)中看到任何内容。我查看了根目录中的 default.aspx 页面,但那似乎与此无关。

谢谢

8个回答

160

查看 Default.aspx/Default.aspx.cs 和 Global.asax.cs 文件。

你可以设置一个默认路由:

        routes.MapRoute(
            "Default", // Route name
            "",        // URL with parameters
            new { controller = "Home", action = "Index"}  // Parameter defaults
        );

只需将控制器/操作名称更改为所需的默认值即可。这应该是路由表中的最后一条路由。


6
@NikolaiDante,你应该把那个评论变成一个回答,因为我差点错过了它,而且回答方式更迅速。 :) 谢谢。 - GazB
3
在MVC 5中,如果您有一个表单登录,当您在首页点击登录时,它仍会重定向到Home控制器,而不是路由中指定的自定义控制器。注册操作也会做类似的事情。因此除了更改RouteConfig之外,还需要更改一些代码,例如调用RedirectionToAction("Index","Home")并将其替换为您自己的控制器和操作名称。 - anIBMer
值得一提的是,您可以拥有多个路由。这可以是默认路由,其中包含空白URL参数,但您可能需要第二个路由,例如url: "{controller}/{action}/{id}"。只需为路由命名即可。 - Jess
这个答案可能适合你,但如果你需要多条路线,请看这里https://dev59.com/12oy5IYBdhLWcg3wkOwK#8470531 - Jess
1
此答案仅适用于MVC 3及更早版本。请参见下面的答案,了解推荐的MVC 4及更高版本方法。 - JTW
该文件现已位于名为 RouteConfig.csApp_Start 文件夹中。 - jjasspper

26

ASP.NET Core

路由配置在Startup类的Configure方法中。要设置“主页”,只需添加以下内容。当用户导航到您网站的基本URL,即yoursite.com时,这将导致用户被路由到MapRoute方法中定义的控制器和操作,例如,yoursite.com将路由用户到yoursite.com/foo/index:

app.UseMvc(routes =>
{
   routes.MapRoute(
   name: "default",
   template: "{controller=FooController}/{action=Index}/{id?}");
});

ASP.NET Core之前

在App_Start/RouteConfig.cs (MVC 3和4)或Global.asax.cs (MVC 1和2)中使用RegisterRoutes方法,如下所示。如果用户导航到您站点的基本URL,即yoursite.com,则会将其路由到MapRoute方法中定义的控制器和操作,例如yoursite.com将路由用户到yoursite.com/foo/index:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // Here I have created a custom "Default" route that will route users to the "YourAction" method within the "FooController" controller.
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "FooController", action = "Index", id = UrlParameter.Optional }
    );
}

5

步骤1:在您的解决方案中单击Global.asax文件。

步骤2:然后转到RouteConfig.RegisterRoutes(RouteTable.Routes);的定义。

步骤3:更改控制器名称和视图名称。

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(name: "Default",
                        url: "{controller}/{action}/{id}",
                        defaults: new { controller = "Home", 
                                        action = "Index", 
                                        id = UrlParameter.Optional }
                        );
    }
}

4

MVC 5中的属性路由

在MVC 5之前,您可以通过在RouteConfig.cs文件中调用routes.MapRoute(...)来将URL映射到特定的操作和控制器。这是主页的URL地址(Home/Index)所存储的位置。但是,如果您修改默认路由如下所示,

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

请记住,这将影响其他操作和控制器的URL。例如,如果您有一个名为ExampleController的控制器类,并且其中有一个名为DoSomething的操作方法,则预期的默认URL ExampleController/DoSomething 将不再可用,因为默认路由已更改。
解决方法是不要修改默认路由,并在RouteConfig.cs文件中为其他操作和控制器创建新路由,如下所示,
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
    name: "Example",
    url: "hey/now",
    defaults: new { controller = "Example", action = "DoSomething", id = UrlParameter.Optional }
);

现在,ExampleController类的DoSomething操作将映射到URL hey/now。但是,每次您想要为不同的操作定义路由时,这可能变得很繁琐。因此,在MVC 5中,您现在可以添加属性来匹配URL和操作,如下所示:
public class HomeController : Controller
{
    // url is now 'index/' instead of 'home/index'
    [Route("index")]
    public ActionResult Index()
    {
        return View();
    }
    // url is now 'create/new' instead of 'home/create'
    [Route("create/new")]
    public ActionResult Create()
    {
        return View();
    }
}

4
public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",               
            defaults: new { controller = "Your Controller", action = "Your Action", id = UrlParameter.Optional }
        );
    }
}

3

请检查global.asax.cs文件中的RegisterRoutes方法 - 这是路由配置的默认位置...


1
我尝试了这个答案,但对我没有用。这是我最终做的事情:
创建一个新的控制器DefaultController,在index操作中,我写了一行重定向:
return Redirect("~/Default.aspx")

在RouteConfig.cs文件中,更改路由的controller="Default"。
 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
        );

你可能忘记在控制器名称中省略“Controller”一词,从而创建了默认路由。 - amarax

0

如果你不想更改路由器,请转到HomeController并在索引中更改MyNewViewHere,如下所示:

    public ActionResult Index()
    {
        return View("MyNewViewHere");
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接