【问题标题】:ASP .Net custom route not workingASP .Net 自定义路由不起作用
【发布时间】:2013-03-07 05:49:22
【问题描述】:

ASP .Net 自定义路由不起作用。

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

       //custom route
        routes.MapRoute(
         "Admin",
         "Admin/{addressID}",// controller name with parameter value only(exclude parameter name)
         new { controller = "Admin", action = "address" }
       new { addressID = @"\d+" }
     );
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }


    public ActionResult address(int addressID = 0)
    {
     //code and redirection
    }

如果可能,我想在这里隐藏 url 中的所有内容......就像我想尽可能隐藏动作名称和参数名称和值...... 建议我这样做的可能方法

就像我想要这样的网址(优先考虑)
1.http://localhost:abcd/Admin
或者 2.http://localhost:abcd/Admin/地址
或者 3.http://localhost:abcd/Admin/1
或者 4.http://localhost:abcd/Admin/address/1

【问题讨论】:

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-routing global-asax


【解决方案1】:

供快速参考。

  • 自定义路由应出现在默认路由之前。
  • 尝试将您的自定义路由命名为空。 routes.MapRoute( null, // Route name...
  • 检查您调用的操作是否正确。
  • 如果您正在处理在初始加载时不接收参数的操作(例如分页) 确保您的参数可以为空address(int? addressID) 在您的自定义路线上应该是这样的

//custom route
    routes.MapRoute(
     null, //<<--- set to null
     "Admin/{addressID}",// controller name with parameter value only(exclude arameter name)
     new { controller = "Admin", action = "address" }
   //new { addressID = @"\d+" } <<--- no need for this because based from your example " 2.http: //localhost:abcd/Admin/address" the parameter can be null.
 );

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 2016-05-10
    • 1970-01-01
    • 2013-11-04
    • 2019-01-05
    • 1970-01-01
    相关资源
    最近更新 更多