Skip to content

Instantly share code, notes, and snippets.

@hancheester
Created October 31, 2019 00:55
Show Gist options
  • Save hancheester/72adac9e70892c3a1d81af2a1c7d9172 to your computer and use it in GitHub Desktop.
Save hancheester/72adac9e70892c3a1d81af2a1c7d9172 to your computer and use it in GitHub Desktop.
public static class UrlHelperExtensions
{
public static string GenerateAbsoluteUrl(this UrlHelper helper, string path, bool forceHttps = false)
{
const string HTTPS = "https";
var uri = helper.RequestContext.HttpContext.Request.Url;
var scheme = forceHttps ? HTTPS : uri.Scheme;
var host = uri.Host;
var port = (forceHttps || uri.Scheme == HTTPS) ? string.Empty : (uri.Port == 80 ? string.Empty : ":" + uri.Port);
return string.Format("{0}://{1}{2}/{3}", scheme, host, port, string.IsNullOrEmpty(path) ? string.Empty : path.TrimStart('/'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment