我问类似的问题here https://stackoverflow.com/questions/10069687/use-htmlhelper-to-get-action-in-beginform-method-of-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
在 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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)