在ASP.NET中,如何将〜/ Application / Page.aspx转换为http:// localhost:45 / MyApp / Application / Page.aspx

时间:2012-07-05 20:00:41

标签: asp.net

基本上我想将虚拟路径转换为绝对路径。

1 个答案:

答案 0 :(得分:1)

试试这个

/// <summary>
/// Return full path of the IIS application
/// </summary>
public string FullyQualifiedApplicationPath
{
    get
    {
        //Getting the current context of HTTP request
        var context = HttpContext.Current;

        //Checking the current context content
        if (context == null) return null;

        //Formatting the fully qualified website url/name
        var 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;
    }
}