使用ASP.NET Core获取绝对URL [英] Getting absolute URLs using ASP.NET Core
问题描述
在MVC 5中,我具有以下扩展方法来生成绝对URL,而不是相对URL:
In MVC 5, I had the following extension methods to generate absolute URLs, instead of relative ones:
public static class UrlHelperExtensions
{
public static string AbsoluteAction(
this UrlHelper url,
string actionName,
string controllerName,
object routeValues = null)
{
string scheme = url.RequestContext.HttpContext.Request.Url.Scheme;
return url.Action(actionName, controllerName, routeValues, scheme);
}
public static string AbsoluteContent(
this UrlHelper url,
string contentPath)
{
return new Uri(url.RequestContext.HttpContext.Request.Url, url.Content(contentPath)).ToString();
}
public static string AbsoluteRouteUrl(
this UrlHelper url,
string routeName,
object routeValues = null)
{
string scheme = url.RequestContext.HttpContext.Request.Url.Scheme;
return url.RouteUrl(routeName, routeValues, scheme);
}
}
ASP.NET Core相当于什么?
What would the equivalent be in ASP.NET Core?
-
UrlHelper.RequestContext
不再存在. - 您将无法获得
HttpContext
的所有权,因为不再有静态的HttpContext.Current
属性.
UrlHelper.RequestContext
no longer exists.- You can't get hold of the
HttpContext
as there is no longer a staticHttpContext.Current
property.
据我所知,您现在还需要传入HttpContext
或HttpRequest
对象.我对吗?有什么方法可以控制当前请求?
As far as I can see, you would now require the HttpContext
or HttpRequest
objects to be passed in also. Am I right? Is there some way to get hold of the current request?
即使在正确的轨道上,域现在也应该是一个环境变量,该变量可以简单地附加到相对URL吗?这会是更好的方法吗?
Am I even on the right track, should the domain now be an environment variable, which is simple appended to the relative URL? Would this be a better approach?
推荐答案
从ASP.NET Core 1.0开始
/// <summary>
/// <see cref="IUrlHelper"/> extension methods.
/// </summary>
public static class UrlHelperExtensions
{
/// <summary>
/// Generates a fully qualified URL to an action method by using the specified action name, controller name and
/// route values.
/// </summary>
/// <param name="url">The URL helper.</param>
/// <param name="actionName">The name of the action method.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <param name="routeValues">The route values.</param>
/// <returns>The absolute URL.</returns>
public static string AbsoluteAction(
this IUrlHelper url,
string actionName,
string controllerName,
object routeValues = null)
{
return url.Action(actionName, controllerName, routeValues, url.ActionContext.HttpContext.Request.Scheme);
}
/// <summary>
/// Generates a fully qualified URL to the specified content by using the specified content path. Converts a
/// virtual (relative) path to an application absolute path.
/// </summary>
/// <param name="url">The URL helper.</param>
/// <param name="contentPath">The content path.</param>
/// <returns>The absolute URL.</returns>
public static string AbsoluteContent(
this IUrlHelper url,
string contentPath)
{
HttpRequest request = url.ActionContext.HttpContext.Request;
return new Uri(new Uri(request.Scheme + "://" + request.Host.Value), url.Content(contentPath)).ToString();
}
/// <summary>
/// Generates a fully qualified URL to the specified route by using the route name and route values.
/// </summary>
/// <param name="url">The URL helper.</param>
/// <param name="routeName">Name of the route.</param>
/// <param name="routeValues">The route values.</param>
/// <returns>The absolute URL.</returns>
public static string AbsoluteRouteUrl(
this IUrlHelper url,
string routeName,
object routeValues = null)
{
return url.RouteUrl(routeName, routeValues, url.ActionContext.HttpContext.Request.Scheme);
}
}
奖金提示
您不能直接在DI容器中注册IUrlHelper
.解决IUrlHelper
的实例要求您使用IUrlHelperFactory
和IActionContextAccessor
.但是,您可以将以下操作作为快捷方式:
Bonus Tip
You can't directly register an IUrlHelper
in the DI container. Resolving an instance of IUrlHelper
requires you to use the IUrlHelperFactory
and IActionContextAccessor
. However, you can do the following as a shortcut:
services
.AddSingleton<IActionContextAccessor, ActionContextAccessor>()
.AddScoped<IUrlHelper>(x => x
.GetRequiredService<IUrlHelperFactory>()
.GetUrlHelper(x.GetRequiredService<IActionContextAccessor>().ActionContext));
ASP.NET Core待办事项
更新:这不会使ASP.NET Core 5
ASP.NET Core Backlog
UPDATE: This won't make ASP.NET Core 5
种种迹象表明,您可以使用LinkGenerator
来创建绝对URL,而无需提供HttpContext
(这是LinkGenerator
的最大缺点,也是为什么IUrlHelper
的缺点,尽管使用设置起来更为复杂下面的解决方案更易于使用),请参见>轻松配置绝对URL的主机/方案与LinkGenerator" .
There are indications that you will be able to use LinkGenerator
to create absolute URL's without the need to provide a HttpContext
(This was the biggest downside of LinkGenerator
and why IUrlHelper
although more complex to setup using the solution below was easier to use) See "Make it easy to configure a host/scheme for absolute URLs with LinkGenerator".
这篇关于使用ASP.NET Core获取绝对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!