12

我的应用程序托管在不同的服务器上,我想获取当前服务器上页面的 URL。

你如何在后面的代码中获得这个属性?

4

5 回答 5

31
string url = HttpContext.Current.Request.Url.AbsoluteUri;

http://thehost.com/dir/Default.aspx

string path = HttpContext.Current.Request.Url.AbsolutePath;

/dir/Default.aspx

string host = HttpContext.Current.Request.Url.Host;

thehost.com

于 2013-07-22T13:15:26.483 回答
8

从文件后面的代码中获取 URL 的另一种方法

public string FullyQualifiedApplicationPath
{
    get
    {
        //Return variable declaration
        var appPath = string.Empty;

        //Getting the current context of HTTP request
        var context = HttpContext.Current;

        //Checking the current context content
        if (context != null)
        {
            //Formatting the fully qualified website url/name
            appPath = string.Format("{0}://{1}{2}{3}",
                                    context.Request.Url.Scheme,
                                    context.Request.Url.Host,
                                    context.Request.Url.Port == 80
                                        ? string.Empty
                                        : ":" + context.Request.Url.Port,
                                    context.Request.ApplicationPath);
        }

        if (!appPath.EndsWith("/"))
            appPath += "/";

        return appPath;
    }
}

检查此链接您将获得更多信息。

于 2013-07-22T11:16:18.967 回答
5
string MyUrl = HttpContext.Current.Request.Url.AbsoluteUri
于 2013-07-22T11:03:36.763 回答
5

字符串路径 = HttpContext.Current.Request.Url.AbsolutePath;

于 2013-07-22T11:06:08.083 回答
1
public string GetApplicationName(){
    string url = HttpContext.Current.Request.Url.AbsoluteUri;
    int intStartIndex = GetIndex(url, 3);
    int intEndIndex = GetIndex(url, 4);
    return url.Substring(intStartIndex, (intEndIndex - intStartIndex) - 1);
}

public int GetIndex(string str, int indexNo){
    int index = 0;
    for (int i = 0; i < indexNo; i++){
        int tempIndex = str.IndexOf("/") + 1;
        str = str.Remove(0, tempIndex);
        index += tempIndex;
    }
    return index;
}
于 2014-11-17T13:25:31.033 回答