Web站点的根得到基本URL(绝对/相对URL) [英] getting base url of web site's root (absolute/relative url)

查看:194
本文介绍了Web站点的根得到基本URL(绝对/相对URL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要彻底了解如何在静态和动态文件中使用的相对和绝对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.

这是我的aspx和ASCX文件夹

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

这是我的js文件(这将是同时使用的ASPX和ASCX文件):

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

这是我的静态网页地址(我想展示一些图片和里面的一些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

这是我的图像文件夹

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

如果我想要写和图像文件在我的ASPX文件链接我应该写

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";

但是,如果我想要写的路径硬codeD或JavaScript文件,它应该是什么样的URL地址的?

But if i want to write the path hard coded or from javascript file, what kind of url address it should be?

推荐答案

〜运算符是asp.net只为服务器控件和服务器code认可。你不能使用〜运营商客户的元素。

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.

要克服这些缺点,包括ASP.NET Web应用程序根目录运算符(〜),您可以在指定服务器控件的路径时使用。 ASP.NET解决了〜运营商当前应用程序的根目录。您可以使用〜操作符与文件夹一起指定基于当前根目录的路径。

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.

至于您发布的例子

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

以上code将使该服务器的物理路径(例如 - C:\\的Inetpub \\ wwwroot的\\ mysite的\\影像\\ 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"; 

要引用JavaScript中,你可能要考虑一个级别的文件夹结构,以统一访问路径的资源。例如:

To reference resources from javascript you may want to consider a one level folders structure to unify access paths. for example:



  • JS​​

  • Pix的

  • 等...

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

For more details visit asp.net web site paths

这篇关于Web站点的根得到基本URL(绝对/相对URL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆