asp.net MVC 2中的相对url自动重写
ASP.net MVC 2 确实会将 标记中的所有相对 url 重写为完整的相对路径,这很好,但它仅适用于
中编写的 URL。仅
标记,而不是 标记或任何其他元素。
- 创建一个 MVC 2 Web 应用程序
- ,创建任何控制器并在视图内为其创建一个视图,
- 创建一个
标签,如下所示
- 运行您的应用程序,导航到您创建的视图,然后查看源代码,
您会发现 MVC 已将标记中的 url 重写为完整 url,例如:
<link href="../Views/Home/Text.xml" type="text/css" />
我知道该文件位于 Views 文件夹中并且由于 web.config 文件阻止对那里的文件的任何请求而无法查看,但这不是我的问题
如何让 MVC 不仅在 中重写所有 url塔格?
任何帮助将不胜感激。
ASP.net MVC 2 does rewrite all your relative urls in the <link>
tag to the full relative path, which is good but it only works for URLs written in the <link>
tag only, not <script>
tags or any other elements.
- Create an MVC 2 web application
- create any controller and a view for it
- inside the view create a
<link>
tag like this<link href="test.xml" type="text/css"/>
- run your application, navigate to the view you created and then view source
you will find that MVC has rewritten your url in tag to full url like:
<link href="../Views/Home/Text.xml" type="text/css" />
i know that this file is in the Views folder and can't be viewed due to the web.config file that blocks any requests to files there, but thats not my problem
How can i get MVC to rewrite all urls not only in the <link>
tage ?
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况是因为您的
标记具有
runat="server"
属性(来自 WebForms 的令人讨厌的遗产)。删除它就不会发生重写。另外:在处理 url 时,您应该始终使用 Url 帮助程序:
您不应该让自动重写发生,始终使用
Url.Content
来链接静态资源。This happens because your
<head>
tag has arunat="server"
attribute (a nasty heritage from WebForms). Remove it and no rewrites will happen. Also instead of:you should always use Url helpers when dealing with urls:
You should not leave automatic rewrites to happen, always use
Url.Content
for linking static resources.也许你已经尝试过类似的方法
将 html 渲染为
perhaps you have already tried something like this
renders html as