我有一个使用UrlHelper的MvcHtmlString Helper,但是在一种情况下,URL无法返回所需的URL,我不知道为什么。
如果我通过IllustrationsController调用_Navigation.cshtml
,例如@Html.Partial("_Navigation", Model)
,则获得URL /Admin/Illustrations/Test/11979?documentId=12242&customerId=8
,但如果我通过HomeController调用_Navigation.cshtml
,则获得URL {{ 1}}
为什么管理区URL会显示查询字符串?
请在下面查看我的代码:
RouteConfig.cs
/Home/Test/11979/12242/8
HomeController.cs
routeCollection.MapRoute("Test", "{controller}/{action}/{id}/{documentId}/{customerId}", new { controller = "Home", action = "Test", documentId = UrlParameter.Optional, customerId = UrlParameter.Optional });
routeCollection.MapRoute("AdminTest", "Admin/{controller}/{action}/{id}/{documentId}/{customerId}", new { controller = "Illustrations", action = "Test", documentId = UrlParameter.Optional, customerId = UrlParameter.Optional }, new[] { "Documents.Areas.Admin.Controllers" });
IllustrationsController
public ViewResult Test(int id, int? documentId, int? customerId)
{
return View();
}
_Navigation.cshtml
public ViewResult Test(int id, int? documentId, int? customerId)
{
return View();
}
HtmlExtender.cs
var controller = ViewContext.RouteData.GetRequiredString("controller");
var isHome = controller == "Home";
@Html.MenuLink("Test", isHome ? "Home" : "Illustrations", "Test", isHome ? null : "Admin", "Test", new { id = Model.Id, documentId = Model.DocumentId, customerId = Model.CustomerId })