asp.net新手问题-css href路径
当启动我的 asp.net 网站的调试模式时,它会在我的浏览器中呈现 url
http://localhost:111/mywebsite/Default.aspx
css 文件在 html 中像这样引用
<link href="~/css/style.css" runat="server" rel="stylesheet" type="text/css" />
所以自然地该网站会中断,因为它在 localhost:111/css 中查找 CSS 文件/
而不是 localhost:111/mywebsite/css
/。
当我启动网站时,它实际上会从 url 提供服务:
http://mywebsite.com
那么有没有一种方法可以在我的开发和生产区域中使用简单的前缀(例如 < )正确引用我的样式表/代码> 还是什么?
其他信息
在我的解决方案资源管理器中,我看到我的项目标有路径 C:\...\mywebsite
。这就是 ~
假设我的项目始终位于子目录中的原因吗?我如何告诉 Visual Studios 该项目应始终以 http://localhost:111/Default.aspx
的形式提供?
这是我在本地主机和生产服务器的页面源中看到的内容: 。 css 在生产环境中处于活动状态,但在我的本地主机上则不是。
When start debug mode of my asp.net website, it renders in my browser with the url
http://localhost:111/mywebsite/Default.aspx
The css file is referenced like this in the html
<link href="~/css/style.css" runat="server" rel="stylesheet" type="text/css" />
So naturally the site breaks because it looks for the CSS file in localhost:111/css/
instead of localhost:111/mywebsite/css
/.
When I launch the website, it will actually be served from the url:
http://mywebsite.com
So is there a way to reference my stylesheet properly in both my dev and production area with a simple prefix like <?=$site_url ?>
or something?
Additional info
In my solution explorer, I see that my project is marked with the path C:\...\mywebsite
. Is that why the ~
assumes my project is always in a subdirectory? How do I tell Visual Studios that this project should always be served as something like http://localhost:111/Default.aspx
?
This is what I see in the page source of both my localhost and production server:<link href="~/css/style.css" rel="stylesheet" type="text/css"></link>
. The css is active on production, but not my localhost.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我转到解决方案资源管理器,然后选择树中标记为
C:\...\mywebsite
的项目,然后转到 VS 右下角的“属性”窗口,并将虚拟路径更改为<代码>/。现在我的开发网站的根目录与我的产品网站的根目录相同。I went to the solution explorer, then selected the item in the tree labelled
C:\...\mywebsite
, then I went to the Properties window at bottom right of VS, and changed the Virtual path to/
. Now my dev website's root is the same as my prod website's root.这样就可以解决问题了。
This will do the trick.
您已经有了,当页面呈现时,
~
解析为网站的根目录。进一步阅读:http://weblogs.asp.net/fmarguerie/archive/2004/05/05/avoiding-problems-with-relative-and-absolute-urls-in-asp-net.aspx(适用于 chrome、FF 和 IE)。
~
在服务器上解析(这就是您需要runat="server"
的原因)。运行此代码是为了生成来自 HTTP 请求的响应,该请求已到达您的开发服务器,该服务器从磁盘上的项目位置托管该站点。
~
不假设该站点位于子目录中,开发服务器将知道该网站的根目录。在生产中,站点由 IIS 从服务器计算机磁盘上的另一个位置托管。
You already have,
~
resolves to the root of the website when the page is rendered.Further reading: http://weblogs.asp.net/fmarguerie/archive/2004/05/05/avoiding-problems-with-relative-and-absolute-urls-in-asp-net.aspx (which works in chrome, FF and IE).
~
resolves on the server (which is why you needrunat="server"
).This code is run to generate the response from a HTTP request that has come to your dev server which is hosting the site from the location of the project on your disk.
~
doesn't assume that the site is in a subdirectory, the dev server will know the root of the website.In production the site is being hosted by IIS from another location on the disk of the server machine.
将其更改为:
删除
runat="server"
(如果您不需要它)。而不是
href="~/css/style.css"
使用:href="/css/style.css"
< /strong>href="/css/style.css"
表示您网站的root下有一个名为css的文件夹和一个文件其中名为 style.css 。更新:
正如评论所指出的,此解决方案仅在您的应用程序在 root 中运行时才有效。
Change it to:
Remove
runat="server"
(if you don't need it).Instead of
href="~/css/style.css"
use:href="/css/style.css"
href="/css/style.css"
means that there is a folder named css in root of your website and a file named style.css inside it.UPDATE:
As pointed out by comments, this solution only works if your app is running in root.