【问题标题】:MVC RouteConfig, Route determinated controllersMVC RouteConfig,路由确定的控制器
【发布时间】:2015-04-06 17:30:53
【问题描述】:

我只想通过 {action}{id} 访问我的主要页面(索引、关于、联系...),但通过 {controller}{action}{id} 访问其他页面。例如:

A principal page: myweb.com/index
No principal page: myweb.com/Account/Login

我已经搜索过,但我不知道具体是怎么做的。

这是我的路由配置

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

        /*routes.MapRoute(
            "Account",
        "Account/{AccountId}",
        new { action = "Index", controller = "Course" }
        );*/

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

        /*routes.MapRoute(
        "Account",
        "Account/{courseId}",
            //new { action = "Index", controller = "Course" }
        new { controller = "Home", action = "Index" }
        );*/
        //routes.Add()
    }
}

【问题讨论】:

    标签: asp.net-mvc-4 asp.net-mvc-routing


    【解决方案1】:

    如下更改您的路由配置,但您必须将所有主要操作都放在一个控制器中,比如 PrincipalController,因为您的路由中不会有控制器。

    始终在低优先级路由之前映射高优先级路由。

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.MapRoute(
                name: "Principal",
                url: "{action}/{id}",
                defaults: new { controller = Princpal", action = "Index", id = UrlParameter.Optional }
    
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
        }
    }
    

    【讨论】:

      【解决方案2】:

      请看例子:

      public class RouteConfig
      {
          public static void RegisterRoutes(RouteCollection routes)
          {
              routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
      
              // Principal pages have unique route per each page.
              // Methods About and Contact should be implemented in HomeController.
              routes.MapRoute(
                  name: "About",
                  url: "about",
                  defaults: new { controller = "Home", action = "About" }
              );
      
              routes.MapRoute(
                  name: "Contact",
                  url: "contact",
                  defaults: new { controller = "Home", action = "Contact" }
              );
      
              // other pages use this default route
              routes.MapRoute(
                  name: "Default",
                  url: "{controller}/{action}/{id}",
                  defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
              );
          }
      

      【讨论】:

      • 看来我不能使用 2 MapRoute。我能怎么做?我已经使用原始 MapRoute 和您的第二个 MapRoute 示例进行了测试。
      • 您需要在Default 之前添加我的示例中的about 路由定义。
      猜你喜欢
      • 2016-03-23
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 2018-02-20
      • 2018-03-26
      相关资源
      最近更新 更多