Relative path not working when website is deployed

梦想的初衷 提交于 2019-12-22 08:57:26

问题


I have a website that has a relative path to a stylesheet that looks like this "/stylesheets/main.css". Now this works fine when I run the site in Visual Studio. But when I deploy the site to our Windows Server 2003 the path stops working. If I go back into code and change the path from "/stylesheets/main.css" to "stylesheets/main.css", the site works fine on the server. I have another website on a different server that uses the same path style ("/stylesheets/main.css") and stylesheet and works with no problems. I really don't want to change all the paths, and am not even sure if this is a problem with the code or the server. Any help or ideas would be great. Thanks.


回答1:


Is the site deployed to the domain's root? If the site is at

http://example.com/somefolder/

then the path /stylesheet/main.css will be interpreted as

http://example.com/stylesheet/main.css

rather than

http://example.com/somefolder/stylesheet/main.css

As @Kit indicated, you can work around this by resolving the path to your application's folder. I have often done this in ASP.NET as follows:

<link rel="stylesheet" type="text/xss" href="<%= ResolveUrl("~/stylesheet/main.css") %>"/>

If that's not the problem, you're going to have to give a bit more detail.




回答2:


In ASP.NET, many times you will need to use a tilde (~) to get the application's root directory, so your paths would look like ~/stylesheets/main.css

When you specify a path that starts with / you are indicating the server root so if you have you site in a virtual directory, it will not be taken into account, but if the site is hosted as the default site, the path will qualify:

Example: server named foo.net with site hosted in a virtual directory named app /stylesheet will translate to foo.net/stylesheet not foo.net/app/stylesheet




回答3:


It would be better to not use relative path and instead use the full path relative to the server. You can do this by processing the tag of your document at the server. So, something like this:

<head runat="server">
  <link href="~/stylesheet/main.css" type="text/css" rel="stylesheet" />
</head>

The tilde (~) signifies that you are coming from the root directory of the website. The reason this works is because you are processing the head at the server. Note that this will work for any other tag that is processed at the server as well.




回答4:


A URL path of the form

/path_preceded_by_a_slash

is not a relative path... it's an absolute path, indicating that the path search should start at the DocumentRoot and not the directory that contains the requesting document.

Perhaps that's your problem. You should be using relative paths, but you are not doing it correctly.




回答5:


When using an absolute path (eg. /images/cool_image.gif') , the root folder is the application root folder set by IIS.

Just as a tricky aside, if you are using a Visual Studio ASP .NET Web Application project, the root of your application is set when you set up the Web Application in your IIS or Web Hosting control panel.

If you're using a Visual Studio ASP .NET Web Site project, then the application is compiled automatically 'on the fly' and the root is probably the general IIS root folder, since no application needs to be explicitly set up in IIS. This is unlikely to coincide with the actual root of your application, and for that reason these kind of absolute paths should be avoided in 'Web Site' projects.

For a discussion of Visual Studio 'Web Site' versus 'Web Application', google it - it's a hot topic.

Ben




回答6:


I know this is an old thread, but for future readers, i would like to add that, i noticed that sometimes to make virtual paths to work is mandatory to remove the runat="server" part from the head and when dragging any file in to the page that belongs to the main folder you need to remove the "~" or "..." from the path. something like this:

    <!DOCTYPE html>

<html>
<head>
 <script src="javascript/Red/jquery.min.js"></script>
</head>


来源:https://stackoverflow.com/questions/2411151/relative-path-not-working-when-website-is-deployed

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