【问题标题】:MVC route with default action and parameter, several actions by controller带有默认动作和参数的 MVC 路由,控制器的几个动作
【发布时间】:2012-02-29 07:42:31
【问题描述】:

我正在尝试实现这样的路线:

http://mysite.com/portfolio/landscape

http://mysite.com/portfolio/friends 等等...

所以我写了:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "DefaultIndex", // Route name
                "{controller}/{id}", // URL with parameters
                new { action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

效果很好,我可以拥有我的路线 /portfolio/landscape,但我的具有 SignIn、SignOut、Index 操作的帐户控制器不起作用,因为它每次都会被重定向到 Index。

两个都可以吗?

提前谢谢你

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-3 routes


    【解决方案1】:

    尝试在自定义路由中引入约束,否则将无法找到默认路由。

            routes.MapRoute(
                "DefaultIndex", // Route name
                "portfolio/{id}", // URL with parameters
                new { controller="portfolio", action = "Index", id = UrlParameter.Optional }
            );
    

    这样,您只需在路由中映射以“portfolio”开头的 URL,并指定哪个控制器和操作。对其他 URL 的请求由默认路由处理。

    【讨论】:

      【解决方案2】:

      我认为您可以将路由声明的顺序颠倒。

      public static void RegisterRoutes(RouteCollection routes)
          {
              routes.MapRoute(
                  "Default", // Route name
                  "{controller}/{action}/{id}", // URL with parameters
                  new { controller = "Home", action = "Index", id = UrlParameter.Optional }     // Parameter defaults
              );
              routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
              routes.MapRoute(
                  "DefaultIndex", // Route name
                  "{controller}/{id}", // URL with parameters
                  new { action = "Index", id = UrlParameter.Optional } // Parameter defaults
              );
      
      
          }
      

      如果您提及任何控制器和操作,它将转到该控制器和操作,否则它将选择默认值

      【讨论】:

        【解决方案3】:

        假设现有路线有充分的理由,这里有一种方法可以让AccountController 与这些路线相得益彰:

            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        
                routes.MapRoute(
                    "Account", // Account name
                    "account/{action}/{id}", // URL with parameters
                    new { controller = "Account", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                );
        
                routes.MapRoute(
                    "DefaultIndex", // Route name
                    "{controller}/{id}", // URL with parameters
                    new { action = "Index", id = UrlParameter.Optional } // Parameter defaults
                );
                routes.MapRoute(
                    "Default", // Route name
                    "{controller}/{action}/{id}", // URL with parameters
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                );
        
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-07-15
          • 2012-05-01
          • 2010-12-15
          • 1970-01-01
          • 2012-08-14
          • 2016-07-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多