期间不通过我定义了以下路线在我RouteConfig.cs
友好的URL:ASP.NET MVC:参数URL路由
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
路线调试器的输出:
你显示的代码工作正常。你是如何产生网址的? –
@StephenMuecke我手动测试应用程序上面生成的URL上面,但是有没有其他测试方法?我尝试了RedirectToAction和这样的东西,它将以这种模式创建URL('.../CreateChild?parentId = 32'),例如:@Html.ActionLink(“Create New Item”,“CreateChild”,new {parentId = @ ViewBag.ParentID},null)' – VSB
它需要为'@ Html.ActionLink(“Create New Item”,“CreateChild”,“MaterialPaymentRequestSbuItem”,new {parentId = @ ViewBag.ParentID},null) '这将工作正常,并生成'..../Admin/MaterialPaymentRequestSbuItem/CreateChild/23'。如果不是,那么你还没有向我们展示导致问题的其他事情。 –