uza*_*y95 7 javascript asp.net relative-path absolute-path virtual-directory
我想完全理解如何在静态和动态文件中使用相对和绝对URL地址.
~ :
/ :
.. : in a relative URL indicates the parent directory
. : refers to the current directory
/ : always replaces the entire pathname of the base URL
// : always replaces everything from the hostname onwards
Run Code Online (Sandbox Code Playgroud)
在没有虚拟目录的情况下工作时,此示例很简单.但我正在研究虚拟目录.
Relative URI Absolute URI
about.html http://WebReference.com/html/about.html
tutorial1/ http://WebReference.com/html/tutorial1/
tutorial1/2.html http://WebReference.com/html/tutorial1/2.html
/ http://WebReference.com/
//www.internet.com/ http://www.internet.com/
/experts/ http://WebReference.com/experts/
../ http://WebReference.com/
../experts/ http://WebReference.com/experts/
../../../ http://WebReference.com/
./ http://WebReference.com/html/
./about.html http://WebReference.com/html/about.html
Run Code Online (Sandbox Code Playgroud)
我想在下面模拟一个站点,就像我正在处理虚拟目录的项目一样.
这些是我的aspx和ascx文件夹
http://hostAddress:port/virtualDirectory/MainSite/ASPX/default.aspx
http://hostAddress:port/virtualDirectory/MainSite/ASCX/UserCtrl/login.ascx
http://hostAddress:port/virtualDirectory/AdminSite/ASPX/ASCX/default.aspx
Run Code Online (Sandbox Code Playgroud)
这些是我的JS文件(将与aspx和ascx文件一起使用):
http://hostAddress:port/virtualDirectory/MainSite/JavascriptFolder/jsFile.js
http://hostAddress:port/virtualDirectory/AdminSite/JavascriptFolder/jsFile.js
Run Code Online (Sandbox Code Playgroud)
这是我的静态网页地址(我想显示一些图片并在一些js函数中运行):
http://hostAddress:port/virtualDirectory/HTMLFiles/page.html
Run Code Online (Sandbox Code Playgroud)
这是我的图片文件夹
http://hostAddress:port/virtualDirectory/Images/PNG/arrow.png
http://hostAddress:port/virtualDirectory/Images/GIF/arrow.png
Run Code Online (Sandbox Code Playgroud)
如果我想在我的ASPX文件中写入和图像文件的链接,我应该写
aspxImgCtrl.ImageUrl = Server.MapPath("~")+"/Images/GIF/arrow.png";
Run Code Online (Sandbox Code Playgroud)
但是,如果我想写硬编码的路径或从javascript文件,它应该是什么样的URL地址?
asp.net仅为服务器控件和服务器代码识别〜运算符.您不能将〜运算符用于客户端元素.
服务器控件中的绝对和相对路径引用具有以下缺点:
•应用程序之间无法移植绝对路径.如果移动绝对路径指向的应用程序,链接将中断.
•如果将资源或页面移动到不同的文件夹,则客户端元素样式中的相对路径可能难以维护.
为了克服这些缺点,ASP.NET包含Web应用程序根运算符(〜),您可以在服务器控件中指定路径时使用它.ASP.NET将〜运算符解析为当前应用程序的根.您可以将〜运算符与文件夹结合使用,以指定基于当前根目录的路径.
至于您发布的示例
aspxImgCtrl.ImageUrl = Server.MapPath("~")+"/Images/GIF/arrow.png";
Run Code Online (Sandbox Code Playgroud)
上面的代码将呈现服务器物理路径(例如 - c:\ inetpub\wwwroot\mysite\images\gif\arrow.png",这对客户端来说意义不大,
你应该使用它来获得正确的客户端相对路径:
aspxImgCtrl.ImageUrl = "~/Images/GIF/arrow.png";
Run Code Online (Sandbox Code Playgroud)
要从javascript引用资源,您可能需要考虑一个级别的文件夹结构来统一访问路径.例如:
有关更多详细信息,请访问 asp.net网站路径
| 归档时间: |
|
| 查看次数: |
23740 次 |
| 最近记录: |