gpt4 book ai didi

c# - MVC4 RouteConfig 参数返回 null

转载 作者:太空宇宙 更新时间:2023-11-03 11:07:54 25 4
gpt4 key购买 nike

我的预订 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com