web前端

位置:IT落伍者 >> web前端 >> 浏览文章

在MVC下用XML实现breadcrumbs导航栏


发布日期:2022年02月22日
 
在MVC下用XML实现breadcrumbs导航栏

先看下样子)thisstylewidth=; border=>

像这种导航栏(breadcrumbs)在mvc下我们来实现他我们采用XML来实现这个功能

首先做个准备我们编写rounting规则(顺便提一句我们要用到rounting功能所以规则必须写正确不然出不来喔)

代码如下

public static void RegisterRoutes(RouteCollection routes)

{

routesIgnoreRoute({resource}axd/{*pathInfo});

routesMapRoute( inner

// Route name resume/test/inner/{action}/{id}

// URL with parameters

new { controller = inner action = Index id = }

// Parameter defaults

);

routesMapRoute( test

// Route name resume/test/{action}/{id}

// URL with parameters new

{

controller = test action = Index id =

}

// Parameter defaults

);

routesMapRoute( Default // Route name

{controller}/{action}/{id} // URL with parameters new

{

controller = Home action = Index id =

}

new {

controller = ^(?!(test|inner))*$ action = ^(?!test)*$

}

);

}

我们加了两个规则

/resume/test

和/resume/test/inner

编写用到的XML文件注意是树形结构的

在models写个Navigatorxml

<?xml version= encoding=utf ?> <node Title=首页 Description=潘峰的网站 Action=Index Controller=Home>

<node Title=简历 Description=在线简历 Action=Index Controller=Resume>

<node Title=Test Description=Test Action=Index Controller=test>

<node Title=inner Description=inner Action=Index Controller=inner>

</node>

</node>

</node>

</node>

编写我们的类文件来实现Navigator在models写个navigatorHelpercsusing System;

using SystemCollectionsGeneric;

using SystemLinq;

using SystemWeb;

using SystemXml;

using SystemXmlLinq;

using SystemWebRouting;

using SystemWebMvc;

using SystemIO;

using SystemText;

namespace conansoftHelpers

{

public static class MenuHelper

{

private static HttpServerUtilityBase Server = null;

private static HttpRequestBase Request = null;

private static UrlHelper Url = null;

private static RouteValueDictionary RouteDictionary = null;

public static string Navigator(this HtmlHelper helper)

{

Server = helperViewContextRequestContextHttpContextServer;

Request = helperViewContextRequestContextHttpContextRequest;

Url = new UrlHelper(helperViewContextRequestContext);

RouteDictionary = helperViewContextRequestContextRouteDataValues;

string xmlPath = ServerMapPath(UrlContent(~/Models/Navigatorxml));

XDocument doc = XDocumentLoad(xmlPath);

XElement node = FindNode(docRoot);

StringBuilder sb = new StringBuilder();

Stack s = new Stack();

while (node != null)

{

sPush(node);

nodenode = nodeParent;

}

//输出breadcrumbs可以自行修改使之符合你的要求

while (sCount() != )

{

node = sPop();

if (UrlEqual(node))

{

sbAppendLine(stringFormat({} nodeAttribute(Title)Value nodeAttribute(Description)Value));

}

else

{

sbAppendLine(stringFormat({} nodeAttribute(Title)Value

UrlAction(nodeAttribute(Action)Value nodeAttribute(Controller)Value)

nodeAttribute(Description)Value));

sbAppendLine( > );

}

}

return sbToString();

}

/// /// 查找当前节点

/// /// 当前节点

/// 找到返回找不到为空

private static XElement FindNode(XElement e)

{

XElement result = e;

if (UrlEqual(e))

{

return e;

}

else

{

if (eHasElements)

{

foreach (XElement ee in eElements())

{

result = FindNode(ee);

}

}

else

{

return null;

}

return result;

}

}

/// /// Url是否相等

/// /// 节点

private static bool UrlEqual(XElement e)

{

string url = UrlAction(eAttribute(Action)Value eAttribute(Controller)Value)ToLower();

string url = UrlRouteUrl(RouteDictionary)ToLower();

return url == url;

}

}

}

解释一下我们利用xml文件来实现breadcrumbs并且我们用action和controller来判断是否为当前路径[UrlEqual]

在网页中加入

<%=HtmlNavigator() %> 好了效果如下image onmousewheel=javascript:return big(this)  height= alt= hspace= src=http://imgeducitycn/img_///png width= onload=javascript:if(thiswidth>)thisstylewidth=; border=>

我的网站

image onmousewheel=javascript:return big(this)  height= alt= hspace= src=http://imgeducitycn/img_///png width= onload=javascript:if(thiswidth>)thisstylewidth=; border=>

image onmousewheel=javascript:return big(this)  height= alt= hspace= src=http://imgeducitycn/img_///png width= onload=javascript:if(thiswidth>)thisstylewidth=; border=>

               

上一篇:WebForm页面内容输出的细节分析

下一篇:访问WebService传递复杂参数