作为标题所述,面包屑中的一个节点的URL不正确。我只得到http://localhost/#
显然我有问题。我在站点地图中还有其他类似的结构。你能从这个缺少的东西中分辨出来吗?
我可以在需要时发布更多信息。
站点地图:
<mvcSiteMapNode title="ISP" controller="xxx" action="Index">
<mvcSiteMapNode title="PC" action="Details" preservedRouteParameters="pcId">
<mvcSiteMapNode title="SGD" controller="yyy" action="Details" preservedRouteParameters="pcId, yyyId, editable">
<mvcSiteMapNode title="ESGN" controller="yyy" action="Title" preservedRouteParameters="pcId, yyyId, editable" />
</mvcSiteMapNode>
动作:
[HttpGet]
[Route("xxx/{pcId:int}/yyy/{yyyId:int}/Details/{editable:bool}")]
public virtual ActionResult Details(int pcId, int yyyId, bool editable)
{
[HttpGet]
[Route("xxx/{pcId:int}/yyy/{yyyId:int}/Title")]
public virtual ActionResult Title(int pcId, int yyyId)
{
路线图:
routes.MapRoute(
name: "xxx",
url: "xxx/{action}/{pcId}",
defaults: new
{
controller = "xxx",
action = "Index",
pcId = UrlParameter.Optional
}
);
更新:删除"可编辑"参数时,它开始起作用。超过2个参数会有一个问题吗?或可能是参数的类型或名称?
更新后的agnowl88的调试建议:
urlhelper确实会生成正确的URL
这是我的控制器代码:
[HttpGet]
[Route("TransactionDetails/File/{fileId:int}")]
public virtual ActionResult Index(int fileId)
{
{
var urlHelper = new UrlHelper(new System.Web.Routing.RequestContext(this.HttpContext, this.RouteData));
var url = urlHelper.Action("Index", "Transaction",
new System.Web.Routing.RouteValueDictionary { { "id", 678 } });
System.Diagnostics.Debug.WriteLine(url);
}
{
var urlHelper = new UrlHelper(new System.Web.Routing.RequestContext(this.HttpContext, this.RouteData));
var url = urlHelper.Action("Index", "File",
new System.Web.Routing.RouteValueDictionary {{"fileId", 123}});
System.Diagnostics.Debug.WriteLine(url);
}
我得到:/AdministratorConsole/TransactionDetails/678和/indercanceatorConsole/transactionDetails/file/123
这样,助手可以为我生成一个URL,但是MVCSITEMAPPROVIDER仍然不满意。
SiteMap是:
<mvcSiteMapNode title="Transaction Log" controller="TransactionLog" action="Index">
<mvcSiteMapNode title="Transaction Details" controller="Transaction" action="Index" preservedRouteParameters="id">
<mvcSiteMapNode title="File Details" controller="File" action="Index" preservedRouteParameters="id, fileId"> <!--TODO link back to parent not working-->
a #
表明无法根据所提供的信息(当前请求与节点配置中的内容)解决URL。请查看为什么默认URLResolver为解释提供例外。
MvcSiteMapProvider
通过MVC的UrlHelper
类解析URL,因此,如果您遇到麻烦,则应明确使用UrlHelper
来进行故障排除。如果将以下代码放入控制器并将其编辑以匹配生成#
的请求,则可以确定如何正确解析URL。但是,与MvcSiteMapProvider
不同,如果无法解决URL,则UrlHelper
将返回null
。最有可能的原因是您缺少路由为所需的路由值。
// Using controller and action
var urlHelper = new UrlHelper(new System.Web.Routing.RequestContext(this.HttpContext, this.RouteData));
var url = urlHelper.Action("View", "Videos", new System.Web.Routing.RouteValueDictionary { { "id", 123 } });
// Using controller, action, and route name (similar to @Html.RouteLink())
var urlHelper = new UrlHelper(new System.Web.Routing.RequestContext(this.HttpContext, this.RouteData));
var url = urlHelper.RouteUrl("RouteName", new System.Web.Routing.RouteValueDictionary { { "controller", "Videos" }, { "action", "View" }, { "id", 123 } });