使用ASP.NET MVC3在自定义Htmlhelper中查找区域名称和控制器名称 [英] Find Area name and Controller Name in custom Htmlhelper with ASP.NET MVC3

查看:63
本文介绍了使用ASP.NET MVC3在自定义Htmlhelper中查找区域名称和控制器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试重写和自定义@Html.ActionLink,在此方法的重载之一中,参数为:

I try to rewrite and customize @Html.ActionLink, in one of overloads of this method the parameters are:

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, 
                                       string linkText,   string actionName);

我想要类似上面的内容,并且还需要找到AreaName和ControllerName而不通过参数传递它,我想使用以下内容:

And I want something like the above and also need to find AreaName and ControllerName without pass it by parameters, I think to use the followings:

string controller = ViewContext.RouteData.Values["Controller"];
string area = ViewContext.RouteData.DataTokens["Area"];

但是错误上升为:

An object reference is required for the non-static field, method, or property
'System.Web.Mvc.ControllerContext.RouteData.get'

显然我使用静态,所以您对在HtmlHelpers中找到区域名称和控制器名称有何建议?

And obviously I use static, so what is your suggestion to find Area Name and Controller Name in HtmlHelpers ?

推荐答案

使用此:

string controllerName = 
(string)htmlHelper.ViewContext.RouteData.GetRequiredString("controller");

string areaName = 
(string)htmlHelper.ViewContext.RouteData.DataTokens["area"];

这篇关于使用ASP.NET MVC3在自定义Htmlhelper中查找区域名称和控制器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆