Javascript 获取网站根目录的基本网址(绝对/相对网址)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2540021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting base url of web site's root (absolute/relative url)
提问by uzay95
I want to completely understand how to use relative and absolute url address in static and dynamic files.
我想彻底了解如何在静态和动态文件中使用相对和绝对 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
This example is easy when you are working without virtual directory. But i am working on virtual directory.
当您在没有虚拟目录的情况下工作时,此示例很容易。但我正在处理虚拟目录。
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
I want to simulate a site below, like my project which is working on virtual directory.
我想在下面模拟一个站点,就像我在虚拟目录上工作的项目一样。
These are my aspx and ascx folder
这些是我的 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
These are my JS Files(which will be use both with the aspx and ascx files):
这些是我的 JS 文件(将与 aspx 和 ascx 文件一起使用):
http://hostAddress:port/virtualDirectory/MainSite/JavascriptFolder/jsFile.js
http://hostAddress:port/virtualDirectory/AdminSite/JavascriptFolder/jsFile.js
this is my static web page address(I want to show some pictures and run inside some js functions):
这是我的静态网页地址(我想显示一些图片并在一些js函数中运行):
http://hostAddress:port/virtualDirectory/HTMLFiles/page.html
this is my image folder
这是我的图片文件夹
http://hostAddress:port/virtualDirectory/Images/PNG/arrow.png
http://hostAddress:port/virtualDirectory/Images/GIF/arrow.png
if i want to write and image file's link in my ASPX file i should write
如果我想在我的 ASPX 文件中写入图像文件的链接,我应该写
aspxImgCtrl.ImageUrl = Server.MapPath("~")+"/Images/GIF/arrow.png";
But if i want to write the path hard coded or from javascript file, what kind of url address it should be?
但是,如果我想编写硬编码的路径或从 javascript 文件中编写路径,它应该是什么样的 url 地址?
采纳答案by MK.
The ~ operator is recognized by asp.net only for server controls and in server code. You cannot use the ~ operator for client elements.
~ 操作符仅被 asp.net 识别用于服务器控件和服务器代码。您不能对客户端元素使用 ~ 运算符。
Absolute and relative path references in a server control have the following disadvantages:
服务器控件中的绝对和相对路径引用具有以下缺点:
?Absolute paths are not portable between applications. If you move the application that the absolute path points to, the links will break.
?绝对路径在应用程序之间不可移植。如果移动绝对路径指向的应用程序,链接将中断。
?Relative paths in the style of client elements can be difficult to maintain if you move resources or pages to different folders.
? 如果将资源或页面移动到不同的文件夹,客户端元素样式中的相对路径可能难以维护。
To overcome these disadvantages, ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application. You can use the ~ operator in conjunction with folders to specify a path that is based on the current root.
为了克服这些缺点,ASP.NET 包含 Web 应用程序根运算符 (~),您可以在服务器控件中指定路径时使用它。ASP.NET 将 ~ 运算符解析为当前应用程序的根。您可以将 ~ 运算符与文件夹结合使用来指定基于当前根目录的路径。
As for the example you posted
至于你发布的例子
aspxImgCtrl.ImageUrl = Server.MapPath("~")+"/Images/GIF/arrow.png";
the above code will render the server physical path (for example - c:\inetpub\wwwroot\mysite\images\gif\arrow.png" which is meaning less on the client side,
上面的代码将呈现服务器物理路径(例如 - c:\inetpub\wwwroot\mysite\images\gif\arrow.png”,这在客户端意义不大,
you should use this for correct client relative path:
您应该将其用于正确的客户端相对路径:
aspxImgCtrl.ImageUrl = "~/Images/GIF/arrow.png";
To reference resources from javascript you may want to consider a one level folders structure to unify access paths. for example:
要从 javascript 引用资源,您可能需要考虑使用一级文件夹结构来统一访问路径。例如:
- Pages
- JS
- Pix
- etc...
- 页面
- JS
- 像素
- 等等...
For more details visit asp.net web site paths
有关详细信息,请访问 asp.net 网站路径

