[英]In ASP.NET MVC 2 - How do I get Route Values into my navigation controller so I can highlight the current link?
I'm trying to get the current Route into my navigation controller so I can run comparisons as the navigation menu data is populated. 我正在尝试将当前路径导入我的导航控制器,以便在填充导航菜单数据时运行比较。
My Links object is like this: 我的链接对象是这样的:
public class StreamNavLinks
{
public string Text { get; set; }
public RouteValueDictionary RouteValues { get; set; }
public bool IsSelected { get; set; }
}
In the master page, I'm trying to pass the current route to the nav controller like this: 在母版页中,我试图将当前路径传递给导航控制器,如下所示:
<% Html.RenderAction(
"MenuOfStreamEntries", // action
"Nav", // controller
new { // parameters for action
currentStreamUrl = "Blog",
currentRoute = ViewContext.RouteData } // get route data to compare in controller
); %>
The problem that I'm running into is that I can't figure out how to get any of the data out of currentRoute
. 我遇到的问题是我无法弄清楚如何从currentRoute
获取任何数据。 What is the best technique for getting the values out or currentRoute
? 获取值或currentRoute
的最佳技术是什么?
I've tried syntax like this: 我试过这样的语法:
currentRoute.Values.routevaluename
and 和
currentRoute.Values[0]
But I can't seem to get it to work. 但我似乎无法让它发挥作用。
I have also tried placing this code into the action of the navigation controller: 我也尝试将此代码放入导航控制器的操作中:
var current = RouteData["streamUrl"];
and 和
var current = this.RouteData["streamUrl"];
Both versions give me this error: 两个版本都给我这个错误:
Error 1 Cannot apply indexing with [] to an expression of >type 'System.Web.Routing.RouteData' C:\\pathtocontroller\\NavController.cs 25 27 错误1无法将带有[]的索引应用于>类型'System.Web.Routing.RouteData'的表达式'C:\\ pathtocontroller \\ NavController.cs 25 27
It also might be helpful to know the route values that I'm trying to match against: 知道我想要匹配的路由值也可能有所帮助:
routes.MapRoute(null, "", // Only matches the empty URL (i.e. ~/)
new
{
controller = "Stream",
action = "Entry",
streamUrl = "Pages",
entryUrl = "HomePage"
}
);
routes.MapRoute(null, "{streamUrl}/{entryUrl}", // matches ~/Pages/HomePage
new { controller = "Stream", action = "Entry" }
);
So, ultimately mydomain.com/blog/blogentry1 would map to the same controller/action as mydomain.com/pages/welcome. 因此,最终mydomain.com/blog/blogentry1将映射到与mydomain.com/pages/welcome相同的控制器/操作。 I'm not looking for the controller value or the action value, rather I'm looking for streamUrl value and EntryUrl value. 我不是在寻找控制器值或动作值,而是在寻找streamUrl值和EntryUrl值。
You don't need to pass route data to a controller because the controller already has knowledge of it via the RouteData
property: 您不需要将路由数据传递给控制器,因为控制器已通过RouteData
属性了解它:
public ActionResult Index() {
// You could use this.RouteData here
...
}
Now if you want to pass some simple arguments like current action that was used to render the view you could do this: 现在,如果您想传递一些简单的参数,例如用于渲染视图的当前操作,您可以执行以下操作:
<%= Html.RenderAction(
"MenuOfStreamEntries",
"Nav",
new {
currentStreamUrl = "Blog",
currentAction = ViewContext.RouteData.Values["action"],
currentController = ViewContext.RouteData.Values["controller"]
}
); %>
And in your controller: 在你的控制器中:
public ActionResult Nav(string currentAction, string currentController)
{
...
}
Taken from this good article . 摘自这篇好文章 。
Create a custom extension method: 创建自定义扩展方法:
public static class HtmlExtensions
{
public static MvcHtmlString ActionMenuItem(this HtmlHelper htmlHelper, String linkText,
String actionName, String controllerName)
{
var tag = new TagBuilder("li");
if ( htmlHelper.ViewContext.RequestContext
.IsCurrentRoute(null, controllerName, actionName) )
{
tag.AddCssClass("selected");
}
tag.InnerHtml = htmlHelper.ActionLink(linkText, actionName, controllerName).ToString();
return MvcHtmlString.Create(tag.ToString());
}
}
Your HTML markup: 你的HTML标记:
<div id="menucontainer">
<ul id="menu">
<%= Html.ActionMenuItem("Home", "Index", "Home") %>
<%= Html.ActionMenuItem("About", "About", "Home") %>
</ul>
</div>
Produces the following output: 产生以下输出:
<div id="menucontainer">
<ul id="menu">
<li class="selected"><a href="/">Home</a></li>
<li><a href="/Home/About">About</a></li>
</ul>
</div>
Here's a sample method we wrote for doing something similar. 这是我们为做类似事情而编写的示例方法。
public static bool IsCurrentAction(this HtmlHelper helper, string actionName, string controllerName)
{
string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];
return currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.