C#的Web开发。我的文件目录如下图:在App_Code的commonUntil.cs类中,如何得到css目录下的mapStyle.css虚拟路径的全名称(形式为:http://yourserver:端口号/css/mapStyle.css或者http://yourserver/css/mapStyle.css)。最好有一段完整的代码。

解决方案 »

  1.   

    Server.MapPath()这个方法 没记错的话
      

  2.   

    Server.MapPath("css//") + "mapStyle.css"
      

  3.   

    Server.MapPath是通过虚拟路径获得实际路径!!
    要获得虚拟路径不能离开页面来谈,也就是说在App_Code下直接是不能获得虚拟路径的!!
    要获得虚拟路径一定是对应某个页面的!
    你可以在页面获得虚拟路径,作为字符串传到类里进行处理!!
    获得当前页面的虚拟路径代码如下:
    string a=Request.ServerVariables["HTTP_HOST"].ToString();IServiceProvider provider = (IServiceProvider)HttpContext.Current;
    HttpWorkerRequest wr = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
    string b = wr.GetFilePath().ToString();Response.Write("http://"+a+b+"<br>");//当前页面的虚拟路径//以下是你需要的虚拟路径字符串       
    string[] b1 = b.Split('/');
    Response.Write("http://" + a +"/"+ b1[1]+"/css/mapStyle.css");
      

  4.   

    楼上正解!直接用Server.MapPath()得不到的
      

  5.   

    本帖最后由 net_lover 于 2010-01-13 16:21:36 编辑
      

  6.   

    http://localhost:1322/CSharp/cjss.jpg终于出现了期待已久的比硬盘图还强悍的ASP.NET Development Server图...
      

  7.   

    楼上各位都是直接得到http://localhost:1322/CSharp/cjss.jpg 形式,能不能使用相对路径法,到相对于当前页面的路径来得到URL