c# - MVC4 RouteConfig 参数返回 null

标签 c# asp.net-mvc

我的预订 Controller 有以下代码

public ActionResult Index(string id, string name)
{
    return View();
}

和我的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(
            name: "Search",
            url: "{controller}/{location}/{checkIn}/{checkOut}/{no}",
            defaults: new { controller = "Search", action = "Index", location = UrlParameter.Optional, checkIn = UrlParameter.Optional, checkOut = UrlParameter.Optional, no = UrlParameter.Optional }
        );
        routes.MapRoute(
            name: "booking",
            url: "{controller}/{action}/{id}/{name}",
            defaults: new { controller = "Booking", action = "Index", id = UrlParameter.Optional, name=UrlParameter.Optional }
        );}

但是当我访问页面http://localhost:59041/booking/index/1/libin时,两个参数都返回null

最佳答案

see this book

As your application becomes more complex you are likely going to register multiple routes. When you do this its important that you consider the order that that you register them. When the routing engine attempts to locate a matching route, it simply enumerates the collection of routes and it stops enumerating as soon as it find a match.

Add a comment This can cause plenty of problems if you’re not expecting it. Let’s look at an examples where this can be a problem:

routes.MapRoute(
    >     "generic", // Route name
    >     "{site}", // URL with parameters
    >     new { controller = "SiteBuilder", action = "Index" } // Parameter defaults );
    > 
    > routes.MapRoute(
    >     "admin", // Route name
    >     "Admin", // URL with parameters
    >     new { controller = "Admin", action = "Index" } // Parameter defaults );

上面的代码片段注册了两条路由。第一条路线

contains a single placeholder segment and sets the default value of the controller parameter to SiteBuilder. The second route contains a single constant segment and sets the default value of the controller parameter to Admin.

Both of these routes are completely valid, but the order in which they are mapped may cause unexpected problems because the first route matches just about any value entered, which means that it will be the first to match

http://example.com/Admin and since the routing engine stops after finding the first match, the second route would never get used.

So, be sure to keep this scenario in mind and consider the order in which you define custom routes.

首先要写好预订路线

  routes.MapRoute(
            name: "booking",
            url: "{controller}/{action}/{id}/{name}",
            defaults: new { controller = "Booking", action = "Index", id = UrlParameter.Optional, name=UrlParameter.Optional }
        );}
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
        routes.MapRoute(
            name: "Search",
            url: "{controller}/{location}/{checkIn}/{checkOut}/{no}",
            defaults: new { controller = "Search", action = "Index", location = UrlParameter.Optional, checkIn = UrlParameter.Optional, checkOut = UrlParameter.Optional, no = UrlParameter.Optional }
        );

关于c# - MVC4 RouteConfig 参数返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15127776/

相关文章:

c# - 使用 PagedList 对产品列表进行分页

c# - 无法使用 OLEDB C# 创建 Excel 文件

c# - 继承 : only fields and methods?

c# - COM 与 .Net 的互操作性 - 缺少方法/属性

asp.net-mvc - 如何防止 "hello+world"在 mvc 操作中转换为 "hello world"

c# - MVC @RenderSection "sections have been defined but have not been rendered"脚本。多级页面时

asp.net-mvc - 制作多核优化的 ASP.NET MVC 应用程序有哪些有用的技巧?

c# - 如何避免序列化默认值?

c# - 尝试查找 TextBox 中包含的值时出现奇怪的 ASP.NET 问题

c# - System.Web.Mvc 缺少对 ModelClientValidationRule 的引用