1)要訪問有關當前路由信息:
RouteValueDictionary values = Url.RequestContext.RouteData.Values;
然後再通的價值是這樣的:
foreach (KeyValuePair<string, object> pair in values)
{
if (pair.Key.Equals("slug", StringComparison.InvariantCultureIgnoreCase))
2)但是,如果我們需要訪問到整個路線集合,列舉它們或者做我們想做的任何事情(比如從我們的路線建立導航菜單,我這樣做),我們這樣做:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%@ Import Namespace="pending.MVC" %>
<% if (RouteTable.Routes.Count > 0 && Request.IsAuthenticated)
{ %>
<div id="AdminMenu">
<ul>
<%
foreach (NamedTypedRoute r in RouteTable.Routes)
{
if (r.Type == RouteType.Admin)
{
// check if current view is opened in modal mode; if so, correct the admin menu links so
// they open (remain) modal too
bool modal = false;
string append = string.Empty;
if (Request.QueryString["modal"] != null)
modal = bool.Parse(Request.QueryString["modal"]);
if (modal)
append = "?modal=true";
%>
<li><a href="<%=Url.RouteUrl(r.Name) + append%>"><%=r.Name%></a></li>
<%
}
}
%></ul>
</div>
<% } %>
對於這個工作,你還需要一個特殊的NamedTypedRoute類:
public class NamedTypedRoute : Route
{
private string _name;
public RouteType Type { get; set; }
public NamedTypedRoute(string name, RouteType type, string url, IRouteHandler routeHandler)
: base(url, routeHandler)
{
_name = name;
Type = type;
}
public NamedTypedRoute(string name, RouteType type, string url, RouteValueDictionary defaults,
RouteValueDictionary constraints, IRouteHandler routeHandler)
: base(url, defaults, constraints, routeHandler)
{
_name = name;
Type = type;
}
public NamedTypedRoute(string name, RouteType type, string url, RouteValueDictionary defaults,
RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler)
: base(url, defaults, constraints, dataTokens, routeHandler)
{
_name = name;
Type = type;
}
public string Name
{
get { return _name; }
}
}
我看到這個HttpContext.Current到處但是當我嘗試訪問它在我的控制器或查看它不存在。我鍵入HttpContext。列出它並且其中沒有當前屬性。 – mare 2009-11-30 16:10:26
你正在嘗試使用System.Web.Mvc.Controllers.HtttpContext,還有另一個System.Web.HttpContext,試試這個,我認爲應該沒問題..謝謝 – 2009-11-30 16:38:36