用sitemap做主页的菜单栏
首先打开vs--> 新建项-->选择站点地图.


站点地图建好了 其中具体的节点根据自己情况配好就行。
接下来是两个非常重要的类:
using System.Collections.Generic;
using System.Web.Routing; //code from Telerik MVC Extensions
namespace PowerInspact.Menu
{
public class SiteMapNode
{
/// <summary>
/// Initializes a new instance of the <see cref="SiteMapNode"/> class.
/// </summary>
public SiteMapNode()
{
RouteValues = new RouteValueDictionary();
ChildNodes = new List<SiteMapNode>();
} /// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>The title.</value>
public string Title { get; set; } /// <summary>
/// Gets or sets the name of the controller.
/// </summary>
/// <value>The name of the controller.</value>
public string ControllerName { get; set; } /// <summary>
/// Gets or sets the name of the action.
/// </summary>
/// <value>The name of the action.</value>
public string ActionName { get; set; } /// <summary>
/// Gets or sets the route values.
/// </summary>
/// <value>The route values.</value>
public RouteValueDictionary RouteValues { get; set; } /// <summary>
/// Gets or sets the URL.
/// </summary>
/// <value>The URL.</value>
public string Url { get; set; } /// <summary>
/// Gets or sets the child nodes.
/// </summary>
/// <value>The child nodes.</value>
public IList<SiteMapNode> ChildNodes { get; set; } /// <summary>
/// Gets or sets the image path
/// </summary>
/// <value>The name of the image path.</value>
public string ImageUrl { get; set; } /// <summary>
/// Gets or sets the item is visible
/// </summary>
/// <value>A value indicating whether the item is visible</value>
public bool Visible { get; set; }
}
}
//code from Telerik MVC Extensions using System;
using System.IO;
using System.Linq;
using System.Web.Routing;
using System.Xml;
namespace PowerInspact.Menu
{
public class XmlSiteMap
{
public XmlSiteMap()
{
RootNode = new SiteMapNode();
} public SiteMapNode RootNode { get; set; } public virtual SiteMapNode LoadFrom(string physicalPath)
{
string str = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
string content = File.ReadAllText(str + physicalPath); if (!string.IsNullOrEmpty(content))
{
using (var sr = new StringReader(content))
{
using (var xr = XmlReader.Create(sr,
new XmlReaderSettings
{
CloseInput = true,
IgnoreWhitespace = true,
IgnoreComments = true,
IgnoreProcessingInstructions = true
}))
{
var doc = new XmlDocument();
doc.Load(xr); if ((doc.DocumentElement != null) && doc.HasChildNodes)
{
XmlNode xmlRootNode = doc.DocumentElement.FirstChild;
Iterate(RootNode, xmlRootNode);
}
}
}
}
return RootNode;
} private static void Iterate(SiteMapNode siteMapNode, XmlNode xmlNode)
{
PopulateNode(siteMapNode, xmlNode); foreach (XmlNode xmlChildNode in xmlNode.ChildNodes)
{
if (xmlChildNode.LocalName.Equals("siteMapNode", StringComparison.InvariantCultureIgnoreCase))
{
var siteMapChildNode = new SiteMapNode();
siteMapNode.ChildNodes.Add(siteMapChildNode); Iterate(siteMapChildNode, xmlChildNode);
}
}
} private static void PopulateNode(SiteMapNode siteMapNode, XmlNode xmlNode)
{
try
{
//title
siteMapNode.Title = GetStringValueFromAttribute(xmlNode, "title");
//routes, url
string controllerName = GetStringValueFromAttribute(xmlNode, "controller");
string actionName = GetStringValueFromAttribute(xmlNode, "action");
string url = GetStringValueFromAttribute(xmlNode, "url");
if (!string.IsNullOrEmpty(controllerName) && !string.IsNullOrEmpty(actionName))
{
siteMapNode.ControllerName = controllerName;
siteMapNode.ActionName = actionName; }
else if (!string.IsNullOrEmpty(url))
{
siteMapNode.Url = url;
} //image URL
siteMapNode.ImageUrl = GetStringValueFromAttribute(xmlNode, "ImageUrl"); //permission name
var permissionNames = GetStringValueFromAttribute(xmlNode, "PermissionNames"); string pers=System.Web.HttpContext.Current.Session["persioons"].ToString(); string[] arr1 = pers.Substring(, pers.Length-).Split(','); //当前该用户的权限 if (siteMapNode.Title == "组织/企业管理" || siteMapNode.Title == "检测报告" || siteMapNode.Title == "系统设置" || siteMapNode.Title == "标准库" || siteMapNode.Title == "用户设置")
{
string[] arr = permissionNames.Split(',');
if (IsVisible(arr, arr1))
{
siteMapNode.Visible = true;
}
else
{
siteMapNode.Visible = false; ;
}
}
else
{
if (IsVisible(arr1, permissionNames))
{
siteMapNode.Visible = true;
}
else
{
siteMapNode.Visible = false; ;
}
}
}
catch (Exception)
{
} }
public static bool IsVisible(string[] arr, string[] arr1)
{
bool b = false;
for (int i = ; i < arr.Length; i++)
{
for (int j = ; j < arr1.Length; j++)
{
if (arr[i] == arr1[j])
{
b = true;
break;
}
}
}
return b;
}
public static bool IsVisible(string[] arr, string permissionNames)
{
bool b = false;
for (int i = ; i < arr.Length; i++)
{
if (permissionNames == arr[i])
{
b = true;
break;
}
}
return b;
}
private static string GetStringValueFromAttribute(XmlNode node, string attributeName)
{
string value = null; if (node.Attributes.Count > )
{
XmlAttribute attribute = node.Attributes[attributeName]; if (attribute != null)
{
value = attribute.Value;
}
} return value;
}
}
}
这两个类完全可以直接拿去用,只是第二个类里面有一些判断要改一下(登陆成功是否显示),其他都不用动。
最后来看看在母版页里如何调用:
<!-- 左边菜单栏 style can be found in sidebar.less -->
<ul class="sidebar-menu" data-widget="tree">
<li class="header">MAIN NAVIGATION</li> @{
string strUrl = Request.Url.ToString();
//load sitemap 注意命名空间
var siteMap = new PowerInspact.Menu.XmlSiteMap();
PowerInspact.Menu.SiteMapNode Nodes = siteMap.LoadFrom("sitemap.config"); //注意站点地图路劲
string str = "";
if (Nodes.ChildNodes.Count > )
{ for (int i = ; i < Nodes.ChildNodes.Count; i++) //父级
{
if (Nodes.ChildNodes[i].Visible)
{
bool IsActive=false;
string[] Persionns = Nodes.ChildNodes[i].ImageUrl.ToString().Split(',');
for (int k = ; k < Persionns.Length; k++)
{
if (strUrl.Contains(Persionns[k]))
{
IsActive = true;
}
} string ico = "";
if (IsActive)
{
ico = "fa-suitcase";
str += " <li class='active treeview'>";
}
else
{
ico = "fa-cogs";
str += " <li class='treeview'>";
}
if (Nodes.ChildNodes[i].ChildNodes.Count > ) //子级fa fa-cogs fa-suitcase
{ str += "<a href='#'><i class='fa "+ico+"'></i><span>" + Nodes.ChildNodes[i].Title + "</span><span class='pull-right-container'><i class='fa fa-angle-left pull-right'></i></span></a>";
str += "<ul class='treeview-menu left_menu'>";
for (int j = ; j < Nodes.ChildNodes[i].ChildNodes.Count; j++)
{
if (Nodes.ChildNodes[i].ChildNodes[j].Visible)
{
if (strUrl.Contains(Nodes.ChildNodes[i].ChildNodes[j].ControllerName))
{
str += @"<li class='active'><a href='/" + Nodes.ChildNodes[i].ChildNodes[j].ControllerName + "'><i class='fa fa-circle-o'></i>" + Nodes.ChildNodes[i].ChildNodes[j].Title + "</a></li>";
}
else
{
str += @"<li><a href='/" + Nodes.ChildNodes[i].ChildNodes[j].ControllerName + "'><i class='fa fa-circle-o'></i>" + Nodes.ChildNodes[i].ChildNodes[j].Title + "</a></li>";
} }
}
str += "</ul>";
}
str += "</li>"; }
} } ViewData["strhtnl"] = str;
}
@Html.Raw(ViewData["strhtnl"])</ul>
用sitemap做主页的菜单栏的更多相关文章
- QiyeProject SpringMVC 项目 d15866p148.iok.la 主要做主页应用,消息应用不管了 用户微信号有点像乱码的那个是openID 找同伴:在项目的GitHub页面里找提问过的人,还有fork,star的人
消息型应用支持文本.图片.语音.视频.文件.图文等消息类型. 主页型应用只支持文本消息类型,且文本长度不超过20个字. 填写必要信息 URL /QiyeProject/src/org/oms/qiye ...
- 使用Hexo+Github一步步搭建属于自己的博客(进阶)
主题的配置:这里以NexT主题作为题材 1.安装NexT,在其文件夹中鼠标右键,点击Git Base Here.输入命令:git clone https://github.com/iissnan/he ...
- vuejs、eggjs全栈式开发设备管理系统
vuejs.eggjs全栈式开发简单设备管理系统 业余时间用eggjs.vuejs开发了一个设备管理系统,通过mqtt协议上传设备数据至web端实时展现,包含设备参数分析.发送设备报警等模块.收获还是 ...
- iOS的非常全的三方库,插件,大牛博客
转自: http://www.cnblogs.com/zyjzyj/p/6015625.html github排名:https://github.com/trending, github搜索:http ...
- 使用Hexo+Github搭建属于自己的博客(进阶)
主题的配置:这里以NexT主题作为题材 1.安装NexT,在其文件夹中鼠标右键,点击Git Base Here.输入命令:git clone https://github.com/iissnan/he ...
- iOS 强大第三方资源库
Github用法 git-recipesGit recipes in Chinese. 高质量的Git中文教程. lark怎样在Github上面贡献代码 my-git有关 git 的学习资料 giti ...
- vuejs、eggjs、mqtt
vuejs.eggjs.mqtt全栈式开发设备管理系统 vuejs.eggjs.mqtt全栈式开发简单设备管理系统 业余时间用eggjs.vuejs开发了一个设备管理系统,通过mqtt协议上传设备数据 ...
- vuejs、eggjs、mqtt全栈式开发设备管理系统
vuejs.eggjs.mqtt全栈式开发简单设备管理系统 业余时间用eggjs.vuejs开发了一个设备管理系统,通过mqtt协议上传设备数据至web端实时展现,包含设备参数分析.发送设备报警等模块 ...
- Jquery Easy UI初步学习(一)
Easy UI 1.3.2 以前听说Easy UI很不错,当了一个dome,闲着没事就看了一下,也整理一下为了自己更好的记忆,也希望对象我这样小菜有帮助吧 先从后台管理的主页面开始,如要要做主页需要了 ...
随机推荐
- opencv实现坐标旋转(教你框住小姐姐)
一.项目背景 最近在做一个人脸检测项目,需要接入百度AI的系统进行识别和检测.主要流程就是往指定的URL上post图片上去,之后接收检测结果就好了. 百度的检测结果包含这样的信息: left - 人脸 ...
- getnameinfo函数
一.函数原型 #include <netdb.h> int getnamefo(const struct sockaddr *sockaddr, socklen_t addrlen, ch ...
- 局域网内ping [局域网内ip地址]命令详解
一.工作过程 主机A向主机B发送一个ICMP请求报文[类型字段为8,代码字段为0],若收到ICMP回复报 文[类型字段为0,代码字段为0]则说明主机B处于活动状态:若超时未收到回复,则可能是 因为(1 ...
- oracle查看表空间数据文件使用情况
-- 查看表空间数据文件使用情况 select a.*, round(a.usedgb/a.maxgb*100) || '%' usedPer from ( select t.TABLESPACE_N ...
- matplotlib-2D绘图库学习目录
matplotlib的安装和 允许中文及几种字体 散点图 直线和点 子图 点的形状 条形图 堆叠条形图 直方图 颜色和样式字符串 饼状图 画多个图 画网格 线的形状 图例 坐标轴 画注释 ...
- [C++]最小生成元 (Digit Generator, ACM/ICPC Seoul 2005, UVa1583)
Question 例题3-5 最小生成元 (Digit Generator, ACM/ICPC Seoul 2005, UVa1583) 如果x+x的各个数字之和得到y,就是说x是y的生成元.给出n( ...
- Python之进程 3 - 进程池和multiprocess.Poll
一.为什么要有进程池? 在程序实际处理问题过程中,忙时会有成千上万的任务需要被执行,闲时可能只有零星任务.那么在成千上万个任务需要被执行的时候,我们就需要去创建成千上万个进程么?首先,创建进程需要消耗 ...
- 将数据以json字符串格式传到前台请求页面
1.前台ajax方法(这个是在FlowDocAction的add.jsp页面) //序列号 var preFileNo = factoryCode+deptCode+"-"+mod ...
- Django 详解 信号Signal
Django信号 Django中提供了“信号调度”,用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者. Model signals pre_init # ...
- 实验吧 smali文件分析
题目链接:http://www.shiyanbar.com/ctf/1871 好吧,这题真是基础中的基础,基础中的战斗机. 不光没有任何绕弯的地方,逻辑有且仅有一个.... 多说无益,使用工具jadx ...