Sae*_*eid 4 asp.net-mvc razor asp.net-mvc-3
我在这里问一个类似的问题,我认为这是一个非常简单的问题(当然不适合我).我有一个Html帮助器的扩展方法,我需要使用HtmlHelper获取当前视图的URL.有没有人对它有任何想法?
根据您的评论和您链接的原始主题,我认为您希望使用Url帮助程序获取表单操作的URL,但我可能误解了您的需求:
在视图中:
@Url.Action(null) returns the current controller/action
@Url.Action("Action") returns a custom action with current controller
@Url.Action("Action","Controller") returns a custom controller and action
Run Code Online (Sandbox Code Playgroud)
在HTML帮助器中:
public static MvcHtmlString MySpecialHelper(this HtmlHelper htmlHelper)
{
UrlHelper urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext,htmlHelper.RouteCollection);
string url = urlHelper.Action("Controller","Action");
//To get the action based on the current Action/Controller use:
url = urlHelper.Action(htmlHelper.ViewData["action"] as string);
//or
url = urlHelper.Action(null);
return new MvcHtmlString(url);
}
Run Code Online (Sandbox Code Playgroud)
如果要获取有关路径信息的信息,例如调用的控制器或操作方法
您可以RouteData从ViewContext对象访问字典
会是这样的
@ViewContext.RouteData.Values["Controller"]
Run Code Online (Sandbox Code Playgroud)
使用RouteData词典,您可以获得有关控制器,操作和额外参数名称的所有信息,具体取决于您的需要
| 归档时间: |
|
| 查看次数: |
15468 次 |
| 最近记录: |