Skip to content

Instantly share code, notes, and snippets.

@Dkowald
Last active October 25, 2018 08:37
Show Gist options
  • Save Dkowald/383acd462242194024981fbe53a84980 to your computer and use it in GitHub Desktop.
Save Dkowald/383acd462242194024981fbe53a84980 to your computer and use it in GitHub Desktop.
UrlAbsolute
using Microsoft.AspNetCore.Mvc;
namespace kwd.gist {
/// <summary>
/// Provide Absolute versions of URL helper methods.
/// </summary>
/// <remarks>
/// Source: https://gist.github.com/383acd462242194024981fbe53a84980.git
/// These cannot be used to change host or protocol,
/// use inbuilt MVC UrlHelper for that.
/// Based on stuff from Marius Schulz
/// https://blog.mariusschulz.com/2011/06/30/how-to-build-absolute-action-urls-using-the-urlhelper-class
/// Based on SO
/// https://stackoverflow.com/questions/434604/how-do-i-find-the-absolute-url-of-an-action-in-asp-net-mvc
/// Using Microsoft.AspNetCore.Mvc.Core UrlHelperExtensions.cs to find overloads.
/// </remarks>
public static class UrlAbsoluteExtensions
{
public static string ActionAbsolute(this IUrlHelper urlHelper) =>
urlHelper.ActionAbsolute(null, null, null, null);
public static string ActionAbsolute(this IUrlHelper urlHelper, string action) =>
urlHelper.ActionAbsolute(action, null, null, null);
public static string ActionAbsolute(this IUrlHelper urlHelper, string action, object values) =>
urlHelper.ActionAbsolute(action, null, values, null);
public static string ActionAbsolute(this IUrlHelper urlHelper, string action, string controller) =>
urlHelper.ActionAbsolute(action, controller, null, null);
public static string ActionAbsolute(this IUrlHelper urlHelper, string action, string controller, object values) =>
urlHelper.ActionAbsolute(action, controller, values, null);
public static string ActionAbsolute(this IUrlHelper urlHelper, string action, string controller, object values,
string fragment) =>
urlHelper.Action(action, controller, values,
urlHelper.ActionContext.HttpContext.Request.Scheme,
urlHelper.ActionContext.HttpContext.Request.Host.Value,
fragment);
public static string RouteUrlAbsolute(this IUrlHelper urlHelper, object values) =>
urlHelper.RouteUrlAbsolute(null, values, null);
public static string RouteUrlAbsolute(this IUrlHelper urlHelper, string routeName) =>
urlHelper.RouteUrlAbsolute(routeName, null, null);
public static string RouteUrl(this IUrlHelper urlHelper, string routeName, object values) =>
urlHelper.RouteUrlAbsolute(routeName, values, null);
public static string RouteUrlAbsolute(this IUrlHelper urlHelper, string routeName, object values,
string fragment) =>
urlHelper.RouteUrl(routeName, values,
urlHelper.ActionContext.HttpContext.Request.Scheme,
urlHelper.ActionContext.HttpContext.Request.Host.Value,
fragment);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment