开发者

How to get the web app root?

开发者 https://www.devze.com 2023-01-11 04:58 出处:网络
I am using this to get the application root and was wondering if this is a newer better way to do it?

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(UriPart开发者_运维技巧ial.Authority) + HttpContext.Current.Request.ApplicationPath.TrimEnd('/');


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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消