您的位置:首页 > 编程语言 > ASP

asp.net MVC5 sitemap 的使用

2015-08-19 17:53 585 查看
1. 安装Nuget package



2. 打开 mvc.sitemap 添加以下代码:

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0"
xsi:schemaLocation="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0 MvcSiteMapSchema.xsd">

<mvcSiteMapNode title="General" key="General" url="General">

<mvcSiteMapNode title="About" controller="Home" action="About"/>

<mvcSiteMapNode title="Contact" controller="Home" action="Contact"/>

<mvcSiteMapNode title="Home" controller="Home" action="Index"/>

</mvcSiteMapNode>

</mvcSiteMap>


3. 在_Layout.cshtml中遍历mvc sitemap:

   
<!--Only for demo-->
<ul class="nav">
@{
var childSettings = Html.MvcSiteMap().SiteMap.FindSiteMapNodeFromKey("General");
var currentNode = Html.MvcSiteMap().SiteMap.CurrentNode;
foreach (var node in childSettings.ChildNodes)
{
var active = "";
if (currentNode != null && (currentNode.Equals(node) || currentNode.IsDescendantOf(node)))
{
active = "active";
}

<li class="@active">
<a href="@node.Url">@Html.DisplayFor(x => node.Title)</a>
</li>
}
}
</ul>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: