2015-10-06 26 views
1

我想用我的C#文件中定義的路由屬性爲創建XML站點地圖(MVCSiteMap)文件是這樣的:如何獲得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> 

但是路由屬性被忽略,結果是:

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

回答

0

我想用我的C#文件中定義的路由屬性爲創建XML站點地圖(MVCSiteMap)文件

你不能。

節點的目的是定義不同控制器動作之間的父子關係。路由屬性不提供這樣做的方法,因此無法自動在文件中包含每條可能的路由匹配或定義層次結構。

您必須提供節點配置才能填寫MVC不提供的缺失部分。您可以提供節點配置的4種方式之一:

  1. .sitemap XML file
  2. MvcSiteMapNodeAttribute
  3. Dynamic Node Providers
  4. 實現ISiteMapNodeProvider和使用外部DI注入你的實現(見this answer瞭解詳細信息)

我認爲與你想要的最相似的選項是使用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() {} 
} 

其他信息:無法路由集合中找到名爲「第一步」的路由。

路由屬性用於標識按名稱註冊的路由,而不是通過URL。所以要使你的配置工作,你需要命名你的路線。

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

然後在您的節點配置中使用相同的名稱。

<mvcSiteMapNode title="Registration Person" route="TheRouteName" controller="Person" action="Index" /> 
相關問題