在 ASP.NET 中,如何将 ~/应用程序/页面.aspx转换为 http://localhost:45/MyApp/



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

试试这个

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

最新更新