1

I want to use the route attribute defined in my C# files for create the xml site map (MVCSiteMap) file like this:

[RoutePrefix("{culture}/Registration/Person")]
public partial class PersonController : BaseController
{
   [HttpPost]
   [Route("Step1")]
   public ActionResult StartStep1() {}
}

and create an xml file like this:

 <mvcSiteMapNode title="Registration" controller="Person" action="Index" >
  <mvcSiteMapNode title="Registration Person" route="Step1" controller="Person" action="Index" />
</mvcSiteMapNode>

But route attribute is ignored and the result was:

Additional information: A route named 'Step1' could not be found in the route collection.

My web.config file is configured like this:

 <add key="MvcSiteMapProvider_IncludeRootNodeFromSiteMapFile" value="true" />
<add key="MvcSiteMapProvider_AttributesToIgnore" value="" />
<add key="MvcSiteMapProvider_CacheDuration" value="5" />
<add key="MvcSiteMapProvider_UseExternalDIContainer" value="false" />
<add key="MvcSiteMapProvider_ScanAssembliesForSiteMapNodes" value="true" />
hanc
  • 121
  • 8

1 Answers1

0

I want to use the route attribute defined in my C# files for create the xml site map (MVCSiteMap) file

You can't.

The purpose of nodes is to define a parent-child relationship between different controller actions. Route attributes do not provide a way to do this, so there is no way to automatically include every possible match for a route in a .sitemap file or define the hierarchy.

You must provide a node configuration to fill in the missing piece that MVC does not provide. You can provide the node configuration one of 4 ways:

  1. .sitemap XML file
  2. MvcSiteMapNodeAttribute
  3. Dynamic Node Providers
  4. Implement ISiteMapNodeProvider and inject your implementation using external DI (see this answer for details)

I think that the option that is most similar to what you want is to use MvcSiteMapNodeAttribute.

[RoutePrefix("{culture}/Registration/Person")]
public partial class PersonController : BaseController
{
    // NOTE: This assumes you have defined a node on your 
    // Home/Index action with a key "HomeKey"
    [MvcSiteMapNode(Title = "Registration", 
        Key = "RegistrationKey", ParentKey = "HomeKey")]
    public ActionResult Index() {}

    [HttpPost]
    [Route("Step1")]
    [MvcSiteMapNode(Title = "Registration Person", 
        Key = "RegistrationPersonKey", ParentKey = "RegistrationKey")]
    public ActionResult StartStep1() {}
}

Additional information: A route named 'Step1' could not be found in the route collection.

The route attribute is to identify a registered route by name, not by URL. So to make your configuration work, you would need to name your route.

[Route("Step1", Name = "TheRouteName")]

And then use the same name in your node configuration.

<mvcSiteMapNode title="Registration Person" route="TheRouteName" controller="Person" action="Index" />
Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212