我有 Controller 名称:District 和操作名称:Incharges 但我希望 URL 是这样的(带有一些参数的操作名称)
www.example.com/district/incharges/aaa
www.example.com/district/incharges/bbb
www.example.com/district/incharges/ccc
但是,在调试 teamName 时,操作参数中始终返回 NULL。
路由
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"DistrictDetails",
"District/Incharges/{teamName}",
new { controller = "District", action = "Incharges" }
);
Controller
但是,在调试 teamName 时,操作参数中始终返回 NULL。
public class DistrictController : Controller
{
public ActionResult Incharges(string teamName)
{
InchargePresentationVM INPVM = new InchargePresentationVM();
INPVM.InitializePath(teamName, string.Empty);
return View("", INPVM);
}
}
查看
@{
ViewBag.Title = "Index";
}
<h2>Index About</h2>
最佳答案
具体路线你要先声明
routes.MapRoute(
"DistrictDetails",
"District/Incharges/{teamName}",
new { controller = "District", action = "Incharges", id = UrlParameter.Optional }
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
););
关于asp.net-mvc - MVC Controller 操作参数为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21437726/