Как получить рут веб-приложения? - PullRequest
0 голосов
/ 11 августа 2010

Я использую это для получения рута приложения, и мне было интересно, если это более новый лучший способ сделать это?

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

1 Ответ

3 голосов
/ 11 августа 2010

Может показаться, что он идет за борт, но я использую эту вспомогательную процедуру.У меня еще не было проблем с этим.

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;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...