如何将html中的所有链接定向到网站的根文件夹?

问题描述:

由于系统要求我在公司的服务器下开发网站,所以我发现很难将资源重定向到该网站的根目录。如果我使用 /指定根目录,则所有javascript,图像和CSS文件都可以在localhost中完美使用。但是,如果我尝试连接到公司的服务器,则所有失败,因为包含该网站所有资料的文件夹只是该服务器的子目录。我试图使用base链接到根
。它适用于javascript和图像,但是以某种方式无法使用css文件。当我使用inspect元素时,css的链接似乎是../../stylesheet/some.css,而javascript的源似乎是javascript / master.js。我想知道是否有人可以帮助我解决该问题,因为该网站的子文件夹数量非常复杂,并且我无法使用.. / .. / .. / ....来引用资源。下面是我的项目的结构。

Since I am asked to develop a web site under company's server, I find it hard to redirect resources to the root of that website. If I use "/" to specify the root directory, all javascripts, images and css files could be used perfectly in localhost. However, if I try to connect to company's server, everything fails since the folder containing all the materials of that web site is only a subdirectory of that server. I tried to use base to link to the root . It works for javascript and images but somehow css files cannot be used. When I use inspect element, the link of css appears to be ../../stylesheet/some.css while source of javascript appears to be javascript/master.js. I wonder if some one could help me solve the problem since the web site is quite complicated in terms of number of subfolders and it is not possible for me to use ../../../.... to refer to resources. Below is the structure of my project.


  1. In。 aspx
    使用HttpRequest.ApplicationPath(参考 MSDN


< script runat = server>
受保护的void Page_Load(对象发送者,EventArgs e)
{
Label1.Text = Request.ApplicationPath;
Image1.ImageUrl = Request.ApplicationPath + /images/Image1.gif;
Label2.Text = Image1.ImageUrl;
}
< / script>

在.cs
中使用Server.MapPath (〜/ bin)

In .cs Use Server.MapPath("~/bin")