How to direct all the links in html to the root folder of the website?

…衆ロ難τιáo~ 提交于 2020-01-17 03:49:05

问题


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:


  1. In .aspx Use HttpRequest.ApplicationPath (ref MSDN)

    <script runat="server"> protected void Page_Load(object sender, EventArgs e) { Label1.Text = Request.ApplicationPath; Image1.ImageUrl = Request.ApplicationPath + "/images/Image1.gif"; Label2.Text = Image1.ImageUrl; } </script>

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




回答2:


It seems that it is hard to achieve that. The approach I am going to use is by copying the folders to the path. Since "/" is directing to the root of that server, it seems this method is more straightforward. I add an appsetting.

  <appSettings>
    <add key="link" value="/a/b/c/d/e/myfolder/"/>
  </appSettings>

Whenever I need to refer to a resource, I'll use the variable from appsetting to obtain the path. For example,

<link rel="stylesheet" href="<%= ConfigurationManager.AppSettings["link"] %>stylesheet/master.css" />

It is not the best solution but I think it can easily achieve the purpose and is kind of easy to maintain so I just post it here.



来源:https://stackoverflow.com/questions/31689814/how-to-direct-all-the-links-in-html-to-the-root-folder-of-the-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!