asp.net MVC路由配置
asp.net MVC routing configuration
我有一个 ASP.Net MVC 应用程序,其中包含以下路由配置规则:
routes.MapRoute(
"rule1",
"{controller}/{action}/{cid}/{language}/{itemID}/{uid}",
new { controller = "Home", action = "action1" }
, new[] { "MVCApp.Controllers" }
);
我刚刚添加了具有相同数量参数的新操作,我的路由配置更改如下(添加了规则 2):
routes.MapRoute(
"rule2",
"{controller}/{action}/{cid}/{language}/{phoneNumber}/{uid}",
new { controller = "Home", action = "action2" }
, new[] { "MVCApp.Controllers" }
);
routes.MapRoute(
"rule1",
"{controller}/{action}/{cid}/{language}/{itemID}/{uid}",
new { controller = "Home", action = "action1" }
, new[] { "MVCApp.Controllers" }
);
现在当我调用 http://localhost:51650/Home/action2/1/en/1/1
它不会路由到 action1
并抛出异常 The parameters dictionary contains a null entry for parameter 'itemID' of non-nullable type 'System.Int64' for method 'System.Web.Mvc.JsonResult action1(System.String, Int32, Int64, Int64)
.
您必须先声明规则 1,然后声明规则 2。声明路由的顺序对 api 很重要,一旦请求匹配路由,api 将停止搜索路由并转到指定的控制器。
这来自 Pro ASP.NET Web API 本书:
Depending on your needs, you might want to have multiple Web API
routes in your application. This is a totally acceptable option in
ASP.NET Web API, but there are a few important things to be aware of.
If you have multiple routes, the registration order of the routes
matters. When a request comes to the routing level, the route
collection is scanned to find a match. As soon as a match is found,
the search stops, and the remaining routes get ignored. The first
route registered will be looked at first, and so on.
所以由于你的两条路由参数个数相同,所以在第一个符合请求参数个数的路由上退出。要解决这个问题,您可以在路由中添加约束,这样您就可以确保将什么值传递给什么路由,或者将您的路由硬编码到 rule1 中的 action1 和将 action2 硬编码到 rule2,如@Pavel 所述。
对于将 rule1 始终路由到 action1 并将 rule2 始终路由到 actoin2 的操作硬编码,请使用:{controller}/action1/{cid}/{language}/{itemID}/{uid}
用于 action1 并为 action2 和 rule2 执行相同操作
我有一个 ASP.Net MVC 应用程序,其中包含以下路由配置规则:
routes.MapRoute(
"rule1",
"{controller}/{action}/{cid}/{language}/{itemID}/{uid}",
new { controller = "Home", action = "action1" }
, new[] { "MVCApp.Controllers" }
);
我刚刚添加了具有相同数量参数的新操作,我的路由配置更改如下(添加了规则 2):
routes.MapRoute(
"rule2",
"{controller}/{action}/{cid}/{language}/{phoneNumber}/{uid}",
new { controller = "Home", action = "action2" }
, new[] { "MVCApp.Controllers" }
);
routes.MapRoute(
"rule1",
"{controller}/{action}/{cid}/{language}/{itemID}/{uid}",
new { controller = "Home", action = "action1" }
, new[] { "MVCApp.Controllers" }
);
现在当我调用 http://localhost:51650/Home/action2/1/en/1/1
它不会路由到 action1
并抛出异常 The parameters dictionary contains a null entry for parameter 'itemID' of non-nullable type 'System.Int64' for method 'System.Web.Mvc.JsonResult action1(System.String, Int32, Int64, Int64)
.
您必须先声明规则 1,然后声明规则 2。声明路由的顺序对 api 很重要,一旦请求匹配路由,api 将停止搜索路由并转到指定的控制器。
这来自 Pro ASP.NET Web API 本书:
Depending on your needs, you might want to have multiple Web API routes in your application. This is a totally acceptable option in ASP.NET Web API, but there are a few important things to be aware of. If you have multiple routes, the registration order of the routes matters. When a request comes to the routing level, the route collection is scanned to find a match. As soon as a match is found, the search stops, and the remaining routes get ignored. The first route registered will be looked at first, and so on.
所以由于你的两条路由参数个数相同,所以在第一个符合请求参数个数的路由上退出。要解决这个问题,您可以在路由中添加约束,这样您就可以确保将什么值传递给什么路由,或者将您的路由硬编码到 rule1 中的 action1 和将 action2 硬编码到 rule2,如@Pavel 所述。
对于将 rule1 始终路由到 action1 并将 rule2 始终路由到 actoin2 的操作硬编码,请使用:{controller}/action1/{cid}/{language}/{itemID}/{uid}
用于 action1 并为 action2 和 rule2 执行相同操作