Getting absolute URLs using ASP.NET Core
After RC2 and 1.0 you no longer need to inject an IHttpContextAccessor
to you extension class. It is immediately available in the IUrlHelper
through the urlhelper.ActionContext.HttpContext.Request
. You would then create an extension class following the same idea, but simpler since there will be no injection involved.
public static string AbsoluteAction(
this IUrlHelper url,
string actionName,
string controllerName,
object routeValues = null)
{
string scheme = url.ActionContext.HttpContext.Request.Scheme;
return url.Action(actionName, controllerName, routeValues, scheme);
}
Leaving the details on how to build it injecting the accesor in case they are useful to someone. You might also just be interested in the absolute url of the current request, in which case take a look at the end of the answer.
You could modify your extension class to use the IHttpContextAccessor
interface to get the HttpContext
. Once you have the context, then you can get the HttpRequest
instance from HttpContext.Request
and use its properties Scheme
, Host
, Protocol
etc as in:
string scheme = HttpContextAccessor.HttpContext.Request.Scheme;
For example, you could require your class to be configured with an HttpContextAccessor:
public static class UrlHelperExtensions
{
private static IHttpContextAccessor HttpContextAccessor;
public static void Configure(IHttpContextAccessor httpContextAccessor)
{
HttpContextAccessor = httpContextAccessor;
}
public static string AbsoluteAction(
this IUrlHelper url,
string actionName,
string controllerName,
object routeValues = null)
{
string scheme = HttpContextAccessor.HttpContext.Request.Scheme;
return url.Action(actionName, controllerName, routeValues, scheme);
}
....
}
Which is something you can do on your Startup
class (Startup.cs file):
public void Configure(IApplicationBuilder app)
{
...
var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
UrlHelperExtensions.Configure(httpContextAccessor);
...
}
You could probably come up with different ways of getting the IHttpContextAccessor
in your extension class, but if you want to keep your methods as extension methods in the end you will need to inject the IHttpContextAccessor
into your static class. (Otherwise you will need the IHttpContext
as an argument on each call)
Just getting the absoluteUri of the current request
If you just want to get the absolute uri of the current request, you can use the extension methods GetDisplayUrl
or GetEncodedUrl
from the UriHelper
class. (Which is different from the UrLHelper)
GetDisplayUrl. Returns the combined components of the request URL in a fully un-escaped form (except for the QueryString) suitable only for display. This format should not be used in HTTP headers or other HTTP operations.
GetEncodedUrl. Returns the combined components of the request URL in a fully escaped form suitable for use in HTTP headers and other HTTP operations.
In order to use them:
- Include the namespace
Microsoft.AspNet.Http.Extensions
. - Get the
HttpContext
instance. It is already available in some classes (like razor views), but in others you might need to inject anIHttpContextAccessor
as explained above. - Then just use them as in
this.Context.Request.GetDisplayUrl()
An alternative to those methods would be manually crafting yourself the absolute uri using the values in the HttpContext.Request
object (Similar to what the RequireHttpsAttribute does):
var absoluteUri = string.Concat(
request.Scheme,
"://",
request.Host.ToUriComponent(),
request.PathBase.ToUriComponent(),
request.Path.ToUriComponent(),
request.QueryString.ToUriComponent());
For ASP.NET Core 1.0 Onwards
/// <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);
}
}
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 Backlog
UPDATE: This won't make ASP.NET Core 5
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".