如何在Asp.net Web.form中提供正确的文件路径?

时间:2014-12-01 09:09:12

标签: asp.net

在将文件发布到服务器上之后,将文件包含到我的网站中时遇到了麻烦。 website被称为“商店”,位于wwwroot文件夹中。 website不读取脚本,它们的位置如下:shop/app_themes/grey/js/site.js.如何为脚本文件提供正确的路径?我使用~, ../, ../../并没有帮助..

2 个答案:

答案 0 :(得分:0)

用户Server.MapPath方法详情如下

Server.MapPath(path) 

您可以使用Resolve Url方法。

示例

  1. http://www.codeproject.com/Articles/205425/ASP-NET-ResolveUrl-Without-Page
  2. ResolveUrl without an ASP.NET Page

答案 1 :(得分:0)

如果你想要主机网址,那么试试这个

public string GetHostUrl()
            {
                var request = HttpContext.Current.Request;
                var appUrl = HttpRuntime.AppDomainAppVirtualPath;

                if (appUrl == null || appUrl == "" || appUrl != "/")
                {
                    appUrl += "/";
                }

                var baseUrl = string.Format("{0}://{1}{2}", request.Url.Scheme, request.Url.Authority, appUrl);

                return baseUrl;
            }

或者你想要物理路径然后试试这个

public string GetPhysicalPath()
    {
        string PhysicalPath = "";

        if (Application["instancePath"] != null && Convert.ToString(Application["instancePath"]) != "")
        {
            PhysicalPath = Convert.ToString(Application["instancePath"]);
        }
        else
        {
            PhysicalPath = Server.HtmlEncode(Request.PhysicalApplicationPath);
        }

        return PhysicalPath;
    }
}