asp.net中的mappath目录问题 -尊龙游戏旗舰厅官网
发布时间:2025/1/21
17
豆豆
尊龙游戏旗舰厅官网
收集整理的这篇文章主要介绍了
asp.net中的mappath目录问题
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
最近因为要用.net连接access数据库,在数据库路径上遇到了麻烦。 找了半天,没搞懂.net中的目录问题。
在古老的asp中,server.mappath无比好用,使用server.mappath可以很方便的返回物理路径的上一层;而在.net中,返回的是虚拟路径的上一层,郁闷。
比如我们localhost的物理目录是“c:\inetpub\wwwroot\”,我们把“d:\mywebsite\test”设置为web共享, 在“d:\mywebsite\test”下的文件夹“newfolder1”中,有文件“webform1.aspx”。
运行以下代码:
response.write(server.mappath("") "
");
response.write(server.mappath("..") "
");
response.write(server.mappath("../") "
");
response.write(server.mappath("../../") "
");
输出:
d:\mywebsite\test\newfolder1
d:\mywebsite\test
d:\mywebsite\test\
c:\inetpub\wwwroot\ 而在一个asp文件“test.asp”中,有以下代码:
<%
response.write server.mappath("..") "
"
response.write server.mappath("../") "
"
response.write server.mappath("../../") "
"
%>
(需要说明的是,asp代码中response.write server.mappath("")会出现错误。)
输出:
d:\mywebsite\test
d:\mywebsite\test
d:\mywebsite
正解。
所以,asp.net让人郁闷。 并且,不论asp.net还是asp,用mappath方法获得的物理路径,都与当前文件所处的目录有关,所以,在网站中处于不同目录深度的文件不能调用统一的方法获得access数据库的物理路径,实在是很郁闷的问题。
解决的办法,只好是将access数据库的物理路径直接在配置文件中写好,这样可以不必使用mappath方法,也不必受其限制。目前来看,虽然有点笨,但还是最好的方法。
在古老的asp中,server.mappath无比好用,使用server.mappath可以很方便的返回物理路径的上一层;而在.net中,返回的是虚拟路径的上一层,郁闷。
比如我们localhost的物理目录是“c:\inetpub\wwwroot\”,我们把“d:\mywebsite\test”设置为web共享, 在“d:\mywebsite\test”下的文件夹“newfolder1”中,有文件“webform1.aspx”。
运行以下代码:
response.write(server.mappath("") "
");
response.write(server.mappath("..") "
");
response.write(server.mappath("../") "
");
response.write(server.mappath("../../") "
");
输出:
d:\mywebsite\test\newfolder1
d:\mywebsite\test
d:\mywebsite\test\
c:\inetpub\wwwroot\ 而在一个asp文件“test.asp”中,有以下代码:
<%
response.write server.mappath("..") "
"
response.write server.mappath("../") "
"
response.write server.mappath("../../") "
"
%>
(需要说明的是,asp代码中response.write server.mappath("")会出现错误。)
输出:
d:\mywebsite\test
d:\mywebsite\test
d:\mywebsite
正解。
所以,asp.net让人郁闷。 并且,不论asp.net还是asp,用mappath方法获得的物理路径,都与当前文件所处的目录有关,所以,在网站中处于不同目录深度的文件不能调用统一的方法获得access数据库的物理路径,实在是很郁闷的问题。
解决的办法,只好是将access数据库的物理路径直接在配置文件中写好,这样可以不必使用mappath方法,也不必受其限制。目前来看,虽然有点笨,但还是最好的方法。
转载于:https://blog.51cto.com/lcring/33968
总结
以上是尊龙游戏旗舰厅官网为你收集整理的asp.net中的mappath目录问题的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: