使用 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
虽然使用下面的解决方案设置起来更复杂但更容易使用)请参阅 使用 LinkGenerator 轻松配置绝对 URL 的主机/方案".
There are indications that you will be able to use LinkGenerator
to create absolute URLs 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屋!