【问题标题】:Call UrlHelper in models in ASP.NET MVC在 ASP.NET MVC 的模型中调用 UrlHelper
【发布时间】:2011-01-03 04:06:22
【问题描述】:

我需要在 ASP.NET MVC 的模型中生成一些 URL。我想调用类似 UrlHelper.Action() 的东西,它使用路由来生成 URL。我不介意填写通常的空白,例如主机名、方案等。

有什么方法可以调用吗?有没有办法构造一个 UrlHelper?

【问题讨论】:

  • 我自己也在考虑这个问题,但请注意 Url.Action 会生成一个相对 URL。确保这就是你想要的。

标签: asp.net-mvc urlhelper


【解决方案1】:

实用提示,在任何 ASP.NET 应用程序中,都可以获得当前 HttpContext 的引用

HttpContext.Current

源自 System.Web。因此,以下内容可以在 ASP.NET MVC 应用程序中的任何地方工作:

UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);
url.Action("ContactUs"); // Will output the proper link according to routing info

例子:

public class MyModel
{
    public int ID { get; private set; }
    public string Link
    {
        get
        {
            UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);
            return url.Action("ViewAction", "MyModelController", new { id = this.ID });
        }
    }

    public MyModel(int id)
    {
        this.ID = id;
    }
}

在创建的 MyModel 对象上调用 Link 属性将返回有效的 Url 以根据 Global.asax 中的路由查看模型

【讨论】:

  • 你确定有 HttpContext.Current.Request.RequestContext 吗? HttpContext.Current.Request 似乎没有 RequestContext。
  • 这很奇怪。我刚刚测试了这个解决方案,它工作得很好。我正在运行 ASP.NET MVC 2 Preview 2,但我认为这适用于所有版本。不知道为什么它不适合你。您是在 MVC 项目之外创建类吗?还要确保System.WebSystem.Web.Mvc 都有using
  • 我在一个 ASP.NET MVC 1 项目中,我想过缺少使用,但我两个都有。
  • 不太清楚为什么它没有显示。如果其他人可以确认这在 ASP.NET MVC 1 中不存在,那就太好了。我只有一台安装了 VS2010 和 MVC 2 的机器。如果你有兴趣,MVC RC 2 haacked.com/archive/2009/12/16/aspnetmvc-2-rc.aspx
  • 请注意,.NET4+ 支持 Request.RequestContex
【解决方案2】:

我喜欢 Omar 的回答,但这对我不起作用。仅作记录,这是我现在使用的解决方案:

var httpContext = HttpContext.Current;

if (httpContext == null) {
  var request = new HttpRequest("/", "http://example.com", "");
  var response = new HttpResponse(new StringWriter());
  httpContext = new HttpContext(request, response);
}

var httpContextBase = new HttpContextWrapper(httpContext);
var routeData = new RouteData();
var requestContext = new RequestContext(httpContextBase, routeData);

return new UrlHelper(requestContext);

【讨论】:

  • 它包含我网站的 URL。在那里,我删除了它。
  • 考虑到 UrlHelper 类依赖于请求上下文(和 HTTP 上下文),手动构造这些上下文对象可能会产生意想不到的结果。如果 HttpContext.Current 为 null 并且您使用这种方法,我会谨慎行事。
  • 当心这个答案 - 虚拟的 RequestContext 导致 UrlHelper 总是返回空字符串。
【解决方案3】:

UrlHelper 可以在 Controller 操作中使用以下内容构建:

 var url = new UrlHelper(this.ControllerContext.RequestContext);
 url.Action(...);

在控制器之外,可以通过从 RouteTable.Routes RouteData 创建 RequestContext 来构造 UrlHelper。

HttpContextWrapper httpContextWrapper = new HttpContextWrapper(System.Web.HttpContext.Current);
UrlHelper urlHelper = new UrlHelper(new RequestContext(httpContextWrapper, RouteTable.Routes.GetRouteData(httpContextWrapper)));

(基于 Brian 的回答,添加了少量代码更正。)

【讨论】:

  • 但是我的模型中没有控制器。
  • 好的,抱歉,我不确定代码的执行位置。让我看看……
  • 无需创建新的请求上下文:var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
  • 不错@bradlis7。那是 MVC 5 吗?
  • 不起作用,因为this.ControllerContext.RequestContextHttpRequestContext,而UrlHelper 的构造函数需要RequestContext。这两个类是不相关的。
【解决方案4】:

是的,您可以实例化它。您可以执行以下操作:

var ctx = new HttpContextWrapper(HttpContext.Current);
UrlHelper helper = new UrlHelper(
   new RequestContext(ctx,
   RouteTable.Routes.GetRouteData(ctx));

RouteTable.Routes 是一个静态属性,所以你应该没问题;要获得 HttpContextBase 引用,HttpContextWrapper 需要引用 HttpContext,而 HttpContext 会提供该引用。

【讨论】:

  • 这不起作用,尽管它非常接近。请参阅下面的答案。
【解决方案5】:

在尝试了所有其他答案后,我最终得到了

$"/api/Things/Action/{id}"

讨厌的人会讨厌¯\_(ツ)_/¯

【讨论】:

    【解决方案6】:

    我试图在页面内(控制器外)做类似的事情。

    UrlHelper 不允许我像 Pablos 回答那样简单地构建它,但后来我想起了一个有效地做同样事情的老技巧:

    string ResolveUrl(string pathWithTilde)
    

    【讨论】:

      【解决方案7】:

      我认为您正在寻找的是:

      Url.Action("ActionName", "ControllerName");
      

      【讨论】:

        猜你喜欢
        • 2013-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-26
        • 2022-10-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多