asp.net MVC 2中的相对url自动重写

发布于 2024-10-27 00:21:07 字数 647 浏览 9 评论 0原文

ASP.net MVC 2 确实会将 标记中的所有相对 url 重写为完整的相对路径,这很好,但它仅适用于 中编写的 URL。仅 标记,而不是

  1. 创建一个 MVC 2 Web 应用程序
  2. ,创建任何控制器并在视图内为其创建一个视图,
  3. 创建一个 标签,如下所示
  4. 运行您的应用程序,导航到您创建的视图,然后查看源代码,

您会发现 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.

  1. Create an MVC 2 web application
  2. create any controller and a view for it
  3. inside the view create a <link> tag like this <link href="test.xml" type="text/css"/>
  4. 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 技术交流群。

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

发布评论

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

评论(2

吻风 2024-11-03 00:21:07

发生这种情况是因为您的 标记具有 runat="server" 属性(来自 WebForms 的令人讨厌的遗产)。删除它就不会发生重写。另外:

<link href="test.css" type="text/css" />

在处理 url 时,您应该始终使用 Url 帮助程序:

<link href="<%= Url.Content("~/test.css") %>" type="text/css" />

您不应该让自动重写发生,始终使用 Url.Content 来链接静态资源。

This happens because your <head> tag has a runat="server" attribute (a nasty heritage from WebForms). Remove it and no rewrites will happen. Also instead of:

<link href="test.css" type="text/css" />

you should always use Url helpers when dealing with urls:

<link href="<%= Url.Content("~/test.css") %>" type="text/css" />

You should not leave automatic rewrites to happen, always use Url.Content for linking static resources.

檐上三寸雪 2024-11-03 00:21:07

也许你已经尝试过类似的方法

<head runat="server">
 <link href="test.xml" type="text/css"/>
 <script src="<%= ((WebFormView)this.ViewContext.View).ViewPath.Substring(1, 
  ((WebFormView)this.ViewContext.View).ViewPath.LastIndexOf('/')) %>test1.xml" type="text/javascript"></script>
</head>

将 html 渲染为

<head>
 <link href="Views/Shared/test.xml" type="text/css" />
 <script src="/Views/Home/test1.xml" type="text/javascript"></script>
</head>

perhaps you have already tried something like this

<head runat="server">
 <link href="test.xml" type="text/css"/>
 <script src="<%= ((WebFormView)this.ViewContext.View).ViewPath.Substring(1, 
  ((WebFormView)this.ViewContext.View).ViewPath.LastIndexOf('/')) %>test1.xml" type="text/javascript"></script>
</head>

renders html as

<head>
 <link href="Views/Shared/test.xml" type="text/css" />
 <script src="/Views/Home/test1.xml" type="text/javascript"></script>
</head>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文