[英]ASP.NET MVC C# routes.MapRoute not working
I am altering an ASP.NET, MVC, C# application but a routes.MapRoute
entry isn't working as expected. 我正在更改ASP.NET,MVC,C#应用程序,但
routes.MapRoute
条目未按预期工作。 In my Global.asax.cs file I have the following two routes - 在我的Global.asax.cs文件中,我有以下两条路线-
routes.MapRoute(
"MyRoute1", // Route name
"{controller}/{action}/{something}/{name}/{id}/{myParameterA}",
new { controller = "MyController", action = "MyActionA", category = "something", name = "name", id = "id", myParameterA = "myParameterA" });
routes.MapRoute(
"MyRoute2", // Route name
"{controller}/{action}/{something}/{name}/{id}/{myParameterB}",
new { controller = "MyController", action = "MyActionB", category = "something", name = "name", id = "id", myParameterB = UrlParameter.Optional } );
The code in my controller looks like this - 我的控制器中的代码如下所示:
public ActionResult MyActionA(string something, string name, string id, string myParameterA)
{
//do cool stuff!
}
public ActionResult MyActionB(string something, string name, string id, string myParameterB)
{
//do awesome stuff!
}
When I call MyActionB
, the final parameter myParameterB
is coming into the Controller as null even when the parameter is in the URL - (example: /MyController/MyActionB/aThing/aName/123/456). 当我调用
MyActionB
,即使参数位于URL中(例如:/ MyController / MyActionB / aThing / aName / 123/456),最终参数myParameterB
也会作为null进入Controller。
I do want the final parameter ('456' in my above example) to be optional. 我确实希望最终参数(在上面的示例中为“ 456”)是可选的。
MyActionA
is working fine. MyActionA
运行正常。
Any suggestions would be appreciated! 任何建议,将不胜感激! Also, is there a good reference out there on how
routes.MapRoute
works? 另外,关于
routes.MapRoute
工作方式是否有很好的参考? Thank you! 谢谢!
Not sure but I think swap the two around, when you set "myParameterA = "myParameterA"" on the first, you're assigning a default value, when you pass /MyController/MyActionB/aThing/aName/123/456 the url is mapped to the first, but the number 456 is not compatible with the default string value - and so is passed as null. 不确定,但是我想将两者互换一下,当您在第一个设置“ myParameterA =” myParameterA“”时,您将分配一个默认值,当您传递/ MyController / MyActionB / aThing / aName / 123/456时,URL为映射到第一个,但数字456与默认字符串值不兼容-因此作为空值传递。
EDIT: oh and for a good reference, the Apress Pro MVC 3 has an excellent chapter on this - Safari Informit. 编辑:哦,作为参考,Apress Pro MVC 3在Safari Informit上有一章非常出色。
This is because there is nothing to distinguish between those 2 routes once you replace the parameters with strings in the route itself. 这是因为一旦用路由本身中的字符串替换了参数,就无法区分这2条路由。 If you add a static part to the routes you should be able to differentiate between them.
如果在路线上添加静态零件,则应该能够区分它们。
routes.MapRoute(
"MyRoute1", // Route name
"{controller}/{action}/{something}/{name}/{id}/firstroute/{myParameterA}",
new { controller = "MyController", action = "MyActionA", category = "something", name = "name", id = "id", myParameterA = "myParameterA" });
routes.MapRoute(
"MyRoute2", // Route name
"{controller}/{action}/{something}/{name}/{id}/secondroute/{myParameterB}",
new { controller = "MyController", action = "MyActionB", category = "something", name = "name", id = "id", myParameterB = UrlParameter.Optional } );
See if that works. 看看是否可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.