【问题标题】:Set "Homepage" in Asp.Net MVC在 Asp.Net MVC 中设置“主页”
【发布时间】:2010-11-11 15:17:03
【问题描述】:

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

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

我尝试在我的项目中查找 Home/Index,但找不到参考,我在 IIS (6) 中也看不到任何内容。我查看了根目录中的 default.aspx 页面,但这似乎并没有做任何相关的事情。

谢谢

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-routing


    【解决方案1】:

    查看Default.aspx/Default.aspx.cs 和 Global.asax.cs

    您可以设置默认路由:

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

    只需将控制器/动作名称更改为您想要的默认值。那应该是路由表中的最后一条路由。

    【讨论】:

    • @NikolaiDante 您应该将该评论作为答案,因为我几乎错过了它,而且它比这个答案更快。 :) 谢谢
    • 在 MVC 5 中,如果您有表单登录,当您在主页上单击登录时,它仍将重定向到 Home 控制器,而不是您在路由中指定的自定义控制器。注册动作会做类似的事情。所以除了修改routeconfig,还需要修改一些调用RedirectionToAction("Index","Home")的代码,替换成你自己的控制器和动作名称。
    • 值得指出的是,您可以拥有多条路线。这可能是您使用 BLANK URL 参数的默认设置,但您可能需要第二条路由,例如 url: "{controller}/{action}/{id}"。只需给路线起不同的名称。
    • 这个答案可能对您有用,但如果您需要多条路线,请参阅此处stackoverflow.com/a/8470531/1804678
    • 此答案仅适用于 MVC 3 及更早版本。有关推荐的 MVC 4 及更高版本的方法,请参阅下面的答案。
    【解决方案2】:

    ASP.NET 核心

    路由是在Startup 类的Configure 方法中配置的。要设置“主页”,只需添加以下内容。当用户导航到您网站的基本 URL 时,这将导致用户被路由到 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,这将导致用户被路由到 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 }
        );
    }
    

    【讨论】:

      【解决方案3】:

      第 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】:
        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 }
                );
            }
        }
        

        【讨论】:

          【解决方案5】:

          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();
              }
          }
          

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              【解决方案7】:

              我尝试了答案,但它对我不起作用。这就是我最终做的:

              创建一个新的控制器 DefaultController。在索引操作中,我写了一行重定向:

              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 }
                      );
              

              【讨论】:

              • 你可能忘记在控制器名称中省略“控制器”字样,创建默认路由
              【解决方案8】:

              如果不想换路由器,直接去 HomeController 并像这样在索引中更改 MyNewViewHere:

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2012-12-19
                • 1970-01-01
                • 2010-10-18
                • 1970-01-01
                • 2010-10-27
                • 2020-10-21
                • 1970-01-01
                相关资源
                最近更新 更多