Jaggu Jaggu - 1 year ago 123
ASP.NET (C#) Question

How can I get the baseurl of site?

I want to write a little helper method which returns the base url of the site. This is what I came up with:

public static string GetSiteUrl()
string url = string.Empty;
HttpRequest request = HttpContext.Current.Request;

if (request.IsSecureConnection)
url = "https://";
url = "http://";

url += request["HTTP_HOST"] + "/";

return url;

Is there any mistake in this that you can think of. Can you improve upon this?

Answer Source

Try this:

string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + 
    Request.ApplicationPath.TrimEnd('/') + "/";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download