简体   繁体   English

ASP.NET MVC html 编写器错误?

[英]ASP.NET MVC html writer bug?

It's probably a bug.这可能是一个错误。 Steps to reproduce:重现步骤:

(1) Create a ASP.NET MVC 3 project (ASPX view engine) (1)创建一个ASP.NET MVC 3工程(ASPX视图引擎)

(2) Go to the Models folder and create a new simple model (2) Go 到 Models 文件夹并新建一个简单的 model

namespace MvcApp.Models
{
    public class MyModel
    {
        public static string MyString = "foo";        
    }
}

(3) Modify the web.config file, add the namespace of the models, so that you can use models in your views. (3) 修改web.config文件,添加模型的命名空间,以便在视图中使用模型。

      ....
      </controls>
      <namespaces>
        <add namespace="MvcApp.Models" />
      </namespaces>
    </pages>
  </system.web>

(4) Go to /Shared/Site.Master , modify the <head> section. (4) Go 为/Shared/Site.Master ,修改<head>部分。

<head runat="server"> 
    <title><%: MyModel.MyString %><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <link href="<%: MyModel.MyString %>" rel="<%: MyModel.MyString %>" />
    <script src="<%: MyModel.MyString %>" />
</head>

Note runat="server" attribute of head.注意 head 的runat="server"属性。

(5) Run it, and see the source code of the page. (5) 运行,查看页面源代码。 I think you will get(unnecessary spaces are removed):我想你会得到(删除不必要的空格):

<head id="Head1">
   <title>foo Index</title>
   <link href="&lt;%: MyModel.MyString %>" rel="&lt;%: MyModel.MyString %>" />
   <script src="foo" />
</head>

Something interesting:有趣的事情:

a.一个。 All attributes of link tag aren't evaluated correctly.未正确评估link标签的所有属性。 it looks like being encoded?看起来像是被编码了? While the same expressions in title and script are correct.虽然titlescript中的相同表达是正确的。

b.湾。 Change the 3rd line to this:将第 3 行更改为:

<link href="<%: MyModel.MyString %>" rel="<%: string.Format("{0}", "foo") %>" />

OR或者

<link href="<%: MyModel.MyString %>" rel="<%: "foo" %>" />

You will get everything correct(even href is correct.).你会得到一切正确的(甚至href是正确的。)。

c. c。 head without runat="server" is always correct.没有runat="server"head总是正确的。

I think it's a bug when rendering views to html texts.我认为在将视图呈现给 html 文本时这是一个错误。 It's really a nightmare to check the source code and try to find the bug.检查源代码并试图找到错误真的是一场噩梦。 Can anyone tell me the reason?谁能告诉我原因?

The simple reason is that ASP.NET Server control never convert <% %> tags.原因很简单,ASP.NET 服务器控件从不转换 <% %> 标签。 Since your Head contains runat="server", then ASP.NET Web Form Engine takes some interenal decision for every child control whether to make it a web server control or not.由于您的 Head 包含 runat="server",因此 ASP.NET Web 表单引擎会为每个子控件做出一些内部决定,是否将其设为 web 服务器控件。

Put this把这个

<head id="Head1" runat="server"> 
<title><%: MyModel.MyString %><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link id="link1" href="<%: MyModel.MyString %>" rel="<%: MyModel.MyString %>" />
<link id="link2" href="<%: MyModel.MyString %>" rel="<%: string.Format("{0}", "foo") %>" /><%sa %>
<script src="<%: MyModel.MyString %>" />
</head>

<script src="<%: MyModel.MyString %>" />

Now carefully check Show Complete Compilation Source:现在仔细检查Show Complete Compilation Source:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM