Using MvcSiteMapProvider with Attributes and Attribute Routing

I am trying to use MVCSiteMapProvider in my ASP.Net MVC 5 application. Many resources and tutorials can be found, but most of them are related to XML-based customization.

In my application attribute, routing is already in use, and I want to use MvcSiteMapProvider with attributes. But this is not enough, and I have some problems.

For example, I have three actions, as shown below:

//HomeController    
    [Route(@"~/home", Name = "CustomerHomeIndex")]
        [MvcSiteMapNode(Title = "Home Page",  Key = "Home")] 
        public ActionResult Index() {
            return View()
        }

//AccountController       
        [Route(@"~/account", Name = "AccountIndex")]
        [MvcSiteMapNode(Title = "Accounts", ParentKey = "Home", Key = "AccountIndex")] 
        public ActionResult Index() {
        // fetching records from database
            return View();
        }

        [Route(@"~/account-management/{id:int}/{domain:regex(^([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$)}", Name = "AccountDetail")]
        [MvcSiteMapNode(Title = "Account Detail", ParentKey = "AccountIndex", Key = "AccountDetail")] 
        public ActionResult Details(string domain, int id) {
        // fetching record from database by parameters
            return View();
        }

I also added SiteMapPath code to my view

//Details.cshtml
    @Html.MvcSiteMap().SiteMapPath()

But it shows nothing as a result. In my opinion, this is about preservedRouteParameters, but I could not find anything about this parameter used in an attribute MvcSiteMapNode-routed attribute.

, , . - , XML.

+4
1

XML .NET . node (, ) XML. .NET - XML, XML node .

DI:

<appSettings>
    <add key="MvcSiteMapProvider_EnableSiteMapFile" value="false"/>
</appSettings>

DI ( StructureMap):

// Register the sitemap node providers
var siteMapNodeProvider = this.For<ISiteMapNodeProvider>().Use<CompositeSiteMapNodeProvider>()
    .EnumerableOf<ISiteMapNodeProvider>().Contains(x =>
    {
        //Remove the XmlSiteMapNodeProvider
        //x.Type<XmlSiteMapNodeProvider>()
        //    .Ctor<bool>("includeRootNode").Is(true)
        //    .Ctor<bool>("useNestedDynamicNodeRecursion").Is(false)
        //    .Ctor<IXmlSource>().Is(xmlSource);
        x.Type<ReflectionSiteMapNodeProvider>()
            .Ctor<IEnumerable<string>>("includeAssemblies").Is(includeAssembliesForScan)
            .Ctor<IEnumerable<string>>("excludeAssemblies").Is(new string[0]);
    });

, IncludeAssembliesForScan. , NuGet , MvcSiteMapProvider, , MVC, .

DI:

<appSettings>
    <add key="MvcSiteMapProvider_IncludeAssembliesForScan" value="MyAssembly,MyOtherAssembly"/>
</appSettings>

DI:

string[] includeAssembliesForScan = new string[] { "MyAssembly", "MyOtherAssembly" };

... Other code omitted ...

// Register the sitemap node providers
var siteMapNodeProvider = this.For<ISiteMapNodeProvider>().Use<CompositeSiteMapNodeProvider>()
    .EnumerableOf<ISiteMapNodeProvider>().Contains(x =>
    {
        //Remove the XmlSiteMapNodeProvider
        //x.Type<XmlSiteMapNodeProvider>()
        //    .Ctor<bool>("includeRootNode").Is(true)
        //    .Ctor<bool>("useNestedDynamicNodeRecursion").Is(false)
        //    .Ctor<IXmlSource>().Is(xmlSource);
        x.Type<ReflectionSiteMapNodeProvider>()
            .Ctor<IEnumerable<string>>("includeAssemblies").Is(includeAssembliesForScan) // <- Setting is injected here
            .Ctor<IEnumerable<string>>("excludeAssemblies").Is(new string[0]);
    });

, , AttributeRouting. MvcSiteMapProvider , MVC, .

, , , PreservedRouteParameters , , .

[Route(@"~/account-management/{id:int}/{domain:regex(^([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$)}", Name = "AccountDetail")]
[MvcSiteMapNode(Title = "Account Detail", ParentKey = "AccountIndex", Key = "AccountDetail", PreservedRouteParameters="domain,id")] 
public ActionResult Details(string domain, int id) {
// fetching record from database by parameters
    return View();
}

. , , RouteParameters , , . , , MvcSiteMapProvider node . , - ( node), . . MvcSiteMapProvider .

. , , v4.6.15, - DI IStringLocalizer.

, App_GlobalResources. , MVC, , MVC. , .

+3

All Articles