Skip to content

Instantly share code, notes, and snippets.

@zealott
Created October 30, 2017 21:33
Show Gist options
  • Select an option

  • Save zealott/b7ce451022f2a2799fb3e155b458bcab to your computer and use it in GitHub Desktop.

Select an option

Save zealott/b7ce451022f2a2799fb3e155b458bcab to your computer and use it in GitHub Desktop.
How to build your base tag (http://www.w3schools.com/tags/tag_base.asp) -- Added Base tag example from Ryan that checks for SSL cert variable and inserts the proper urlScheme if SSL is set.
// Find the correct base tag
var baseHref = Request.Url.Scheme + "://" + Request.Url.Host + (Request.Url.IsDefaultPort ? "" : ":" + Request.Url.Port.ToString()) + Request.ApplicationPath;
if (!baseHref.EndsWith("/"))
{
baseHref += "/";
}
// Output the HTML
if (!(Model != null && Model.IsPreview))
{
<base href="@baseHref" />
}
//With the movement of SSL being a new web standard we need to start checking to see if SSL certificates are being hosted on a load balancer.
//The below code will check for the variable from a LB and insert the proper scheme if it is, otherwise revert to the current request.
//This is because when SSL is on the load balancer all traffic to the web server will be HTTP, but the client will be getting HTTPS.
var urlScheme = !string.IsNullOrWhiteSpace(Request.ServerVariables["HTTP_X_FORWARDED_PROTO"])
? Request.ServerVariables["HTTP_X_FORWARDED_PROTO"] : Request.Url.Scheme;
string baseUrl = string.Format("{0}://{1}{2}{3}", urlScheme, Request.Url.Host, Request.Url.IsDefaultPort ? "" : ":" + Request.Url.Port, Request.ApplicationPath);
if (!baseUrl.EndsWith("/"))
{
baseUrl += "/";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment