views:

22

answers:

1

Hi guys,

I am using this to get the application root and was wondering if this is a newer better way to do it?

string root = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath.TrimEnd('/');
+2  A: 

It may appear to be going overboard but this is the helper routine I use. I haven't run into any troubles with it yet.

public class UrlHelper
{
    public static string ApplicationBaseUrl()
    {
        string port = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];

        if (port == null || port == "80" || port == "443")
            port = "";
        else
            port = ":" + port;

        string protocol = HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];

        if (protocol == null || protocol == "0")
            protocol = "http://";
        else
            protocol = "https://";

        return protocol + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port +
               HttpContext.Current.Request.ApplicationPath;
    }
}
Ben Griswold
Thanks, I will try this out.
VoodooChild