2

I am working on a MVC5 project where I need to create a sitemap.

I have created my routes using Route Attribute on the controller actions.

I am using a Dynamic node provider as specified in the documentation for MvcSiteMapProvider and it works fine for one of my controllers, however for another one it is displaying the query string paramaters for each mapped route so for example

I am getting

www.url.com?SelectedCulture=en-gb

when I am expecting

www.url.com/en-gb

My routes all work fine and if I use my application it is working fine aswell... It just doesnt create a sitemap which i am expecting

The Dynamic provider that I created is populating the required attribute that matches the route.

Anyone have any ideas?

This is my Mvc.sitemap xml

<mvcSiteMapNode title="ATitle" controller="Parent" action="Index" key="Home">
<mvcSiteMapNode title="ASubTitle" controller="Parent" action="Index" key="CulturePages">
</mvcSiteMapNode>

This is my dynamicNodeProvider

public class SitePagesDynamicProvider : DynamicNodeProviderBase
{
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
    {
        using (var ef = new EntityFrameworkRepository())
        {
            // Create a node for each sites 
            foreach (var obj in ef.GetIDs();
            {
                DynamicNode dynamicNode;

                    dynamicNode = new DynamicNode();
                    dynamicNode.Title = obj.title;
                    dynamicNode.ParentKey = "CulturePages";
                    dynamicNode.RouteValues.Add("SelectedCulture", obj.culture);
                    yield return dynamicNode;
            }
        }
    }
}

And this is my Controller Action:

[MvcSiteMapNodeAttribute(Title = "Sites", Key = "Sites", ParentKey = "HomePages", DynamicNodeProvider = "TestProject.Models.SitePagesDynamicProvider, TestProject")]
    [Route("{SelectedCulture:regex(^([a-z]|[A-Z]){2}-([a-z]|[A-Z]){2}?$)?}/")]
    public ActionResult Index(string SelectedCulture)
    {
       return View();
    }

Thanks!

Bynho
  • 572
  • 6
  • 16

2 Answers2

0

How embarrassing... Issue was down to misspelling of route value id in node provider!

Bynho
  • 572
  • 6
  • 16
0

I have never used AttributeRouting, but as far as I know it works much like declaring your routes in the RouteConfig class.

Routing has 2 parts 1) resolving incoming URLs to match a set of RouteValues and 2) resolving outgoing URLs from a set of RouteValues. It is the outgoing URL (sometimes called outbound URL) that you need to ensure is configured correctly in order for MvcSiteMapProvider to generate the correct URL.

As described in Controlling URL Behavior, the URLs are resolved by the MVC framework's UrlHelper class. You can use the following code in a controller action to see if you can generate the correct URL using MVC and then work backwards. I suggest you start by first configuring your route using the RouteConfig class, ensure you get the correct URL, and then try to find the equivalent expression for AttributeRouting (perhaps by .

// Using controller and action
var urlHelper = new UrlHelper(new System.Web.Routing.RequestContext(this.HttpContext, this.RouteData));
var url = urlHelper.Action("Index", "Parent", new System.Web.Routing.RouteValueDictionary { { "SelectedCulture", "en-gb" } });

Note: You can make MvcSiteMapProvider scale a lot better if you use the built-in localization along with preservedRouteParameters rather than creating nodes for each culture, as described in this answer.

Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212