10. .NET中的路径问题
在多层文件夹中的文件调用与之平行的文件夹中的文件时,要用到(../)来调用,当文件夹很多、很深时,调用文件很麻烦,也很容易出错。现给出一个统一的调用方法:
HTML:/根目录名称(服务器名称)/目标目录/目标文件
CS :~/根目录名称(服务器名称)/目标目录/目标文件
如何得到路径:
- string h1 = HttpContext.Current.Server.MapPath("~");//根目录的物理路径
- string h2 = HttpContext.Current.Server.MapPath(".");//当前目录的物理路径
- string h3 = HttpContext.Current.Request.ApplicationPath;//服务器名
- /// <summary>
- /// 取得网站域名的根目录(绝对路径及相对路径)http://域名/服务器名
- /// </summary>
- /// </summary>
- /// <returns></returns>
- public string GetRootURI()
- {
- string AppPath = "";
- HttpContext HttpCurrent = HttpContext.Current;
- HttpRequest Req;
- if (HttpCurrent != null)
- {
- Req = HttpCurrent.Request;
- string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);
- if (Req.ApplicationPath == null || Req.ApplicationPath == "/")
- //直接安装在 Web 站点
- AppPath = UrlAuthority;
- else
- //安装在虚拟子目录下
- AppPath = UrlAuthority + Req.ApplicationPath;
- }
- return AppPath;
- }
11. 控件包含代码块(即 <% ... %>),因此无法修改控件集合
原因:在程序中使用了JavaScript调用后台的变量和方法,即使用了<%= ..%>,且JavaScript代码放在了<head runat="server"></head>中间,解决方法:把JavaScript代码放在<body></body>中。