呼叫UrlHelper在ASP.NET MVC模型 [英] Call UrlHelper in models in ASP.NET MVC

查看:88
本文介绍了呼叫UrlHelper在ASP.NET MVC模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成在ASP.NET MVC模型的一些网址。我想打电话像UrlHelper.Action(),它使用的路由生成URL。我不介意填补空白平常一样,主机名,计划等。

I need to generate some URLs in a model in ASP.NET MVC. I'd like to call something like UrlHelper.Action() which uses the routes to generate the URL. I don't mind filling the usual blanks, like the hostname, scheme and so on.

有没有我可以调用为任何方法?有没有一种方法来构建UrlHelper?

Is there any method I can call for that? Is there a way to construct an UrlHelper?

推荐答案

我喜欢奥马尔的回答,但是,这不是为我工作。只是为了记录这是我现在使用的解决方案:

I like Omar's answer but that's not working for me. Just for the record this is the solution I'm using now:

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);

这篇关于呼叫UrlHelper在ASP.NET MVC模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆