如何让MVCSiteMap包含我的所有自定义路由

时间:2015-10-06 19:07:41

标签: asp.net routes mvcsitemapprovider

我想使用我的C#文件中定义的route属性来创建xml站点地图(MVCSiteMap)文件,如下所示:

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

并创建一个这样的xml文件:

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

但忽略了route属性,结果为:

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

我的web.config文件配置如下:

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

1 个答案:

答案 0 :(得分:0)

  

我想使用我的C#文件中定义的route属性来创建xml站点地图(MVCSiteMap)文件

你不能。

节点的目的是定义不同控制器操作之间的父子关系。路由属性不提供执行此操作的方法,因此无法自动包含.sitemap文件中路由的每个可能匹配项或定义层次结构。

您必须提供节点配置以填写MVC未提供的缺失部分。您可以通过以下四种方式之一提供节点配置:

  1. .sitemap XML file
  2. MvcSiteMapNodeAttribute
  3. Dynamic Node Providers
  4. 实施ISiteMapNodeProvider并使用外部DI注入您的实施(详见this answer
  5. 我认为与您想要的选项最相似的选项是使用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() {}
    }
    
      

    附加信息:名为&#39; Step1&#39;在路线集合中找不到。

    route属性用于按名称而不是URL标识已注册的路由。因此,为了使您的配置有效,您需要为路线命名。

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

    然后在节点配置中使用相同的名称。

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