开发者

How to get the web app root?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜