0
votes

As the title states, the URL for one of the nodes in the breadcrumb is not correct. I just get a http://localhost/#

Obviously I have something wrong. I have other, similar structures in the sitemap that are working. Can you tell from this whats missing?

I can post more info if needed.

SiteMap :

<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>

Actions:

    [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)
    {

Route Map:

    routes.MapRoute(
            name: "xxx",
            url: "xxx/{action}/{pcId}",
            defaults: new
            {
                controller = "xxx",
                action = "Index",
                pcId = UrlParameter.Optional
            }
            );

Update: When removing the "editable" parameter it started to work. Could there be an issue with more than 2 params? or possibly the type or name of the parameter?

Update following debug advice from NightOwl88:

The urlHelper does generate the correct url's

This is my controller code:

 [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);
        }

I get: /AdministratorConsole/TransactionDetails/678 and /AdministratorConsole/TransactionDetails/File/123

So the helper is able to generate a url for me but MvcSiteMapProvider is still not happy.

SiteMap is:

    <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-->
1

1 Answers

0
votes

A # indicates the URL could not be resolved based on the information provided (between the current request and what is in the node configuration). See Why does Default UrlResolver throw an exception for an explanation.

MvcSiteMapProvider resolves the URL through MVC's UrlHelper class, so if you are having trouble, you should use the UrlHelper explicitly to troubleshoot. If you put the below code into a controller and edit it to match the request that is generating a #, you will be able to determine how to resolve the URL correctly. However, unlike in MvcSiteMapProvider, the UrlHelper will return null if the URL cannot be resolved. The most likely reason is that you are missing a route value that you have set as required by the route.

// 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 } });