开发者

MVC3 ascx vs razor page rendering issue

开发者 https://www.devze.com 2023-02-15 15:56 出处:网络
I have an aspx web page that renders correctly. When converted to razor, it does not. Here is a simplified example (stripped of all extraneous stuff).

I have an aspx web page that renders correctly. When converted to razor, it does not. Here is a simplified example (stripped of all extraneous stuff).

aspx:

   <asp:Content ID="indexContent" Conte开发者_StackOverflow社区ntPlaceHolderID="ToolContent" runat="server">
      <% string test = "<div><b>Tag Test</b></div>"; %>
      <h2><%= test %></h2>
   </asp:Content>

razor:

   @section ToolContent {
      @{ string test = "<div><b>Tag Test</b></div>"; }
      <h2>@test</h2>
   }

The aspx renders as expected. The razor just displays the content of "test" (<div><b>Tag Test</b></div>) in the header tag.

I assume that my understanding of razor is flawed. If someone could enlighten me and/or show me a solution/work around, I would greatly appreciate it.


When you write @test, Razor automatically escapes it.

To prevent it from being escaped, write @Html.Raw(test).

0

精彩评论

暂无评论...
验证码 换一张
取 消