我在 vs 2010 上发布了我的解决方案。 引用样式表和js文件的例子(第一种方法):
<link href="../../Content/CSS/Login.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/ApplicationSecurity/AuthenticateUser.js" type="text/javascript"></script>
这些在调试时工作正常。但是在 iis 7 上浏览时,资源加载失败。在我使用的其他应用程序中(第二种方法):
<link href="~/Content/CSS/Login.css" rel="stylesheet" type="text/css" />
这在 iis 上的调试和浏览期间都工作正常。 查看页面的URL是:
http://localhost/LaunchLogin/
控制台报错:
Failed to load http://localhost/Content/css/Login.css resource: the server responded with a status of 404 (Not Found)
它应该在以下位置搜索文件:
http://localhost/LaunchLogin/Content/css/Login.css
谁能解释iis 是如何尝试访问资源的?另外,我如何配置 iis 或以第一种方法 在 iss 浏览器上运行的方式发布?
最佳答案
使用 ~
基本路径没有问题而且是最好的(正如你所说的;工作完美),但如果你仍然需要相对路径尝试使用这些标签,我删除了路径上的一个目录。
<link href="../Content/CSS/Login.css" rel="stylesheet" type="text/css" />
<script src="../Scripts/ApplicationSecurity/AuthenticateUser.js" type="text/javascript"></script>
顺便说一下,
如果您的 URL 是 http://localhost/LaunchLogin/
并且您的 css 文件位于 http://localhost/LaunchLogin/Content/css/Login.css
那么你链接 css 的代码应该是:
<link href="Content/CSS/Login.css" rel="stylesheet" type="text/css" />
<script src="Scripts/ApplicationSecurity/AuthenticateUser.js" type="text/javascript"></script>
如果您愿意,那么下一个最佳方法是在 CSS 和 JS 链接中使用绝对路径。您可以使用以下技巧来做到这一点:
- 使用一个变量创建一个公共(public)静态类:
public String absRoot='http://localhost/LaunchLogin/';
并将其设置为您网站的任何根目录。 - 现在链接 CSS 和 JS,如下所示:
<link href="<%=StaticClassName.absRoot %>Content/CSS/Login.css" rel="stylesheet" type="text/css" />
<script src="<%=StaticClassName.absRoot %>Scripts/ApplicationSecurity/AuthenticateUser.js" type="text/javascript"></script>
- 之后,无论何时需要,您都可以仅在一处更改
absRoot
值。
关于javascript - 在 iis7 上浏览应用程序时找不到 css 和 js 文件(服务器响应状态为 404),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31017848/