asp.net-mvc - 在 ASP.NET MVC 3 中使用 HtmlHelper 获取当前 View 的 URL

标签 asp.net-mvc asp.net-mvc-3 razor

我问了一个类似的问题 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/

相关文章:

c# - 成就/徽章架构

c# - 你将如何克服这个问题? Jquery + ajax + 服务器超时

c# - 只显示日期不显示时间

asp.net-mvc-3 - SSL 会在我的 asp.net mvc3 web 应用程序中加密查询字符串和发布请求的正文吗

jquery - 如何在 ASP.NET MVC3 中简单地将序列化表单作为 AJAX 表单中的数据输出

C# Razor 静态助手错误 “Collection Was Modified”

c# - ASP.NET MVC 中 ModelState.AddModelError 的关键参数有什么意义?

asp.net - Entity Framework : how to implement shared primary key with Fluent API?

asp.net-mvc - 在 MVC 中一起使用 Ajax.ActionLink 和 ChildActionOnly 失败

asp.net-mvc - MVC Html.ActionLink 未呈现。你能看出我做错了什么吗?