asp.net-mvc 在 ASP.NET MVC 中的模型中调用 UrlHelper
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2031995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call UrlHelper in models in ASP.NET MVC
提问by pupeno
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.
我需要在 ASP.NET MVC 的模型中生成一些 URL。我想调用类似 UrlHelper.Action() 的方法,它使用路由来生成 URL。我不介意填写通常的空白,例如主机名、方案等。
Is there any method I can call for that? Is there a way to construct an UrlHelper?
有什么方法可以调用吗?有没有办法构造一个 UrlHelper?
采纳答案by pupeno
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);
回答by Omar
Helpful tip, in any ASP.NET application, you can get a reference of the current HttpContext
有用的提示,在任何 ASP.NET 应用程序中,您都可以获得当前 HttpContext 的引用
HttpContext.Current
which is derived from System.Web. Therefore, the following will work anywhere in an ASP.NET MVC application:
这是从 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
Example:
例子:
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;
}
}
Calling the Linkproperty on a created MyModel object will return the valid Url to view the Model based on the routing in Global.asax
调用已Link创建的 MyModel 对象上的属性将返回有效的 Url 以根据 Global.asax 中的路由查看模型
回答by Nathan Taylor
A UrlHelper can be constructed from within a Controller action with the following:
UrlHelper 可以从 Controller 操作中构建,如下所示:
var url = new UrlHelper(this.ControllerContext.RequestContext);
url.Action(...);
Outside of a controller, a UrlHelper can be constructed by creating a RequestContext from the RouteTable.Routes RouteData.
在控制器之外,可以通过从 RouteTable.Routes RouteData 创建 RequestContext 来构造 UrlHelper。
HttpContextWrapper httpContextWrapper = new HttpContextWrapper(System.Web.HttpContext.Current);
UrlHelper urlHelper = new UrlHelper(new RequestContext(httpContextWrapper, RouteTable.Routes.GetRouteData(httpContextWrapper)));
(Based on Brian's answer, with a minor code correction added.)
(基于 Brian 的回答,并添加了一个小的代码更正。)
回答by Brian Mains
Yes, you can instantiate it. You can do something like:
是的,您可以实例化它。您可以执行以下操作:
var ctx = new HttpContextWrapper(HttpContext.Current);
UrlHelper helper = new UrlHelper(
new RequestContext(ctx,
RouteTable.Routes.GetRouteData(ctx));
RouteTable.Routesis a static property, so you should be OK there; to get a HttpContextBasereference, HttpContextWrappertakes a reference to HttpContext, and HttpContextdelivers that.
RouteTable.Routes是一个静态属性,所以你应该没问题;获取HttpContextBase引用,HttpContextWrapper引用HttpContext,并HttpContext提供该引用。
回答by Florian Winter
After trying all the other answers, I ended up with
在尝试了所有其他答案后,我最终得到了
$"/api/Things/Action/{id}"
Haters gonna hate ˉ\_(ツ)_/ˉ
讨厌的人会讨厌ˉ\_(ツ)_/ˉ
回答by vGHazard
I was trying to do something similar from within a page (outside of a controller).
我试图从页面内(控制器外)做类似的事情。
UrlHelper did not allow me to construct it as easily as Pablos answer, but then I remembered a old trick to effective do the same thing:
UrlHelper 不允许我像 Pablos 回答那样容易地构建它,但后来我想起了一个老技巧来有效地做同样的事情:
string ResolveUrl(string pathWithTilde)
回答by user246874
I think what you're looking for is this:
我想你要找的是这个:
Url.Action("ActionName", "ControllerName");

