【问题标题】:How to give a correct path to file in Asp.net Web.form?如何在 Asp.net Web.form 中给出正确的文件路径?
【发布时间】:2014-12-01 17:18:36
【问题描述】:

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

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    用户Server.MapPath Methos 详情如下

    Server.MapPath(path) 
    

    你可以使用Resolve Url方法。

    例子

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

    【讨论】:

    • 没有帮助。这将为您提供本地路径,而不是相对服务器 url。
    • 我的 js 文件出现错误“未定义未捕获的引用错误 $(匿名函数)”。会是问题吗?
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-10-22
      • 2018-08-12
      • 1970-01-01
      • 2015-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      • 1970-01-01
      相关资源
      最近更新 更多