我问了一个类似的问题 here我认为这是一个非常简单的方法(当然不适合我)。我有一个 Html helper 的扩展方法,我需要使用 HtmlHelper 获取当前 View 的 url。有没有人对此有任何想法?
最佳答案
根据您的评论和链接到的原始线程,我认为您想使用 Url 助手来获取表单操作的 URL,但我可能误解了您想要的内容:
在 View 中:
@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);
}
关于asp.net-mvc - 在 ASP.NET MVC 3 中使用 HtmlHelper 获取当前 View 的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10073894/