网站部署时相对路径不起作用

发布于 2024-08-24 06:43:31 字数 317 浏览 5 评论 0原文

我有一个网站,它有一个样式表的相对路径,如下所示“/stylesheets/main.css”。现在,当我在 Visual Studio 中运行该网站时,效果很好。但是当我将该站点部署到 Windows Server 2003 时,该路径停止工作。如果我返回代码并将路径从“/stylesheets/main.css”更改为“stylesheets/main.css”,则该站点在服务器上运行良好。我在不同的服务器上有另一个网站,它使用相同的路径样式(“/stylesheets/main.css”)和样式表,并且工作没有问题。我真的不想更改所有路径,甚至不确定这是否是代码或服务器的问题。任何帮助或想法都会很棒。谢谢。

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.

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

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

发布评论

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

评论(7

極樂鬼 2024-08-31 06:43:31

该站点是否部署到域的根目录?如果该站点位于

http://example.com/somefolder/

,则路径 /stylesheet/main.css 将被解释为

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

而不是

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

如 @Kit 所示,您可以通过解析应用程序文件夹的路径来解决此问题。我经常在 ASP.NET 中这样做,如下所示:

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

如果这不是问题,您将必须提供更多细节。

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.

方觉久 2024-08-31 06:43:31

在 ASP.NET 中,很多时候您需要使用波形符 (~) 来获取应用程序的根目录,因此您的路径看起来像 ~/stylesheets/main.css

当您指定以 / 开头的路径时,您正在指示服务器根,因此如果您有如果您的站点位于虚拟目录中,则不会考虑该站点,但如果该站点作为默认站点托管,则该路径将符合条件:

示例:名为 foo.net 的服务器,其站点托管在名为 app 的虚拟目录
/stylesheet 将转换为 foo.net/stylesheet 不是 foo.net/app/stylesheet

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

雾里花 2024-08-31 06:43:31

最好不要使用相对路径,而使用相对于服务器的完整路径。您可以通过在服务器上处理文档的标签来完成此操作。所以,像这样:

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

波浪号 (~) 表示您来自网站的根目录。这样做的原因是因为您正在服务器上处理头部。请注意,这也适用于在服务器上处理的任何其他标签。

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.

我不吻晚风 2024-08-31 06:43:31

表单的 URL 路径

/path_preceded_by_a_slash

不是相对路径...它是绝对路径,指示路径搜索应从 DocumentRoot 开始,而不是包含请求文件。

也许这就是你的问题。您应该使用相对路径,但您做得不正确。

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.

被翻牌 2024-08-31 06:43:31

当使用绝对路径(例如/images/cool_image.gif')时,根文件夹是IIS设置的应用程序根文件夹。

顺便说一句,如果您使用的是 Visual Studio ASP .NET Web 应用程序项目,那么当您在 IIS 或 Web 托管控制面板中设置 Web 应用程序时,就会设置应用程序的根目录。

如果您使用的是 Visual Studio ASP .NET Web Site 项目,则应用程序会“即时”自动编译,并且根目录可能是一般的 IIS 根文件夹,因为不需要任何应用程序在 IIS 中明确设置。这不太可能与应用程序的实际根目录一致,因此在“网站”项目中应避免使用此类绝对路径。

有关 Visual Studio“网站”与“Web 应用程序”的讨论,请在 google 上搜索 - 这是一个热门话题。

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

不疑不惑不回忆 2024-08-31 06:43:31

我知道这是一个旧线程,但对于未来的读者,我想补充一点,我注意到有时要使虚拟路径正常工作,必须从头部删除 runat="server" 部分,并且在将任何文件拖入时对于属于主文件夹的页面,您需要从路径中删除“~”或“...”。
像这样的东西:

    <!DOCTYPE html>

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

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>
八巷 2024-08-31 06:43:31

这对我有用:

<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery-1.8.3.min.js") %>"></script>

that works for me:

<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery-1.8.3.min.js") %>"></script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文