ASP.NET MVC:在URL路由期间不传递参数

时间:2016-05-30 19:01:29

标签: asp.net-mvc asp.net-mvc-5 url-routing asp.net-mvc-areas

我在以下RouteConfig.cs

中定义了以下路线以获得友好的网址
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    name: "CreateChildRoute",
    url: "Admin/MaterialPaymentRequestSbuItem/CreateChild/{parentId}",
    defaults: new { controller = "MaterialPaymentRequestSbuItem", action = "CreateChild", parentId = UrlParameter.Optional }
); 
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces: new[] {"ProjectManager.Controllers"}

);

但是当我打电话时:http://localhost:46813/Admin/MaterialPaymentRequestSbuItem/CreateChild/23 空值传递给控制器​​:

public ActionResult CreateChild(int? parentId){
  .....
} 

但是打电话给http://localhost:46813/Admin/MaterialPaymentRequestSbuItem/CreateChild?parentId=32 没有问题,并按原样传递参数。

P.S。 Admin是我的应用程序中的已定义区域。 以下是http://localhost:46813/Admin/MaterialPaymentRequestSbuItem/CreateChild?parentId=2的Route Debugger的输出: enter image description here

1 个答案:

答案 0 :(得分:3)

从我可以看到你的路线表看起来是正确的,那里有一个拼写错误,但它似乎非常一致,所以我猜这不是问题。

与teo van kot所说的相反,你总是首先想要从最具体的路线开始,并且将更多的一般路线放在最后,因为只会使用匹配的第一条路线。

我用来调试路线的有用工具是Phil Haak的Route Debugger。它可以让你看到选择了什么路线,更有用的是哪些参数被映射,并且可以通过NuGet安装