获取网站根目录的基本 url(绝对/相对 url)

发布于 2024-08-26 20:07:27 字数 2010 浏览 4 评论 0原文

我想完全了解如何在静态和动态文件中使用相对和绝对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

当您在没有虚拟目录的情况下工作时,此示例很简单。但我正在研究虚拟目录。

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

我想模拟下面的一个站点,就像我的项目正在虚拟目录上工作一样。

这些是我的 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

这些是我的 JS 文件(将与 aspx 和 ascx 文件一起使用):

http://hostAddress:port/virtualDirectory/MainSite/JavascriptFolder/jsFile.js
http://hostAddress:port/virtualDirectory/AdminSite/JavascriptFolder/jsFile.js

这是我的静态网页地址(我想显示一些图片并在一些 js 函数中运行):

http://hostAddress:port/virtualDirectory/HTMLFiles/page.html

这是我的图像文件夹

http://hostAddress:port/virtualDirectory/Images/PNG/arrow.png
http://hostAddress:port/virtualDirectory/Images/GIF/arrow.png

,如果我想在我的ASPX文件中写入图像文件的链接,我应该写

aspxImgCtrl.ImageUrl = Server.MapPath("~")+"/Images/GIF/arrow.png";

但是如果我想写入硬编码或来自javascript文件的路径,它应该是什么样的url地址?

I want to completely understand how to use relative and absolute url address in static and dynamic files.

~  : 
/  :
.. : 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

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):

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):

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫九 2024-09-02 20:07:27

ASP.NET 仅识别服务器控件和服务器代码中的 ~ 运算符。您不能对客户端元素使用 ~ 运算符。

服务器控件中的绝对和相对路径引用具有以下缺点:

• 绝对路径不可在应用程序之间移植。如果移动绝对路径指向的应用程序,链接将会中断。

•如果将资源或页面移动到不同的文件夹,则客户端元素样式的相对路径可能难以维护。

为了克服这些缺点,ASP.NET 包含了 Web 应用程序根运算符 (~),您可以在服务器控件中指定路径时使用它。 ASP.NET 将 ~ 运算符解析为当前应用程序的根目录。您可以将 ~ 运算符与文件夹结合使用来指定基于当前根目录的路径。

至于您发布的示例,

aspxImgCtrl.ImageUrl = Server.MapPath("~")+"/Images/GIF/arrow.png";

上面的代码将渲染服务器物理路径(例如 - c:\inetpub\wwwroot\mysite\images\gif\arrow.png",这在客户端意义不大,

您应该使用它正确的客户端相对路径:

aspxImgCtrl.ImageUrl = "~/Images/GIF/arrow.png"; 

要从 javascript 引用资源,您可能需要考虑使用一级文件夹结构来统一访问路径,例如:

  • Pages
  • JS
  • Pix
  • 等...

有关更多详细信息,请访问 asp.net 网站路径

The ~ operator is recognized by asp.net only for server controls and in server code. You cannot use the ~ operator for client elements.

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.

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,

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:

  • Pages
  • JS
  • Pix
  • etc...

For more details visit asp.net web site paths

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文