Created
October 31, 2019 00:55
-
-
Save hancheester/72adac9e70892c3a1d81af2a1c7d9172 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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