在ASP.NET MVC中,用于对文本框内容进行编码的内部方法是什么?

huangapple go评论47阅读模式
英文:

In ASP.NET MVC, what internal method is used to encode textbox contents

问题

这个问题涉及到文本中的 HTML 实体编码。在你提供的代码和文本中,HTML 实体编码用于将特殊字符(如引号和和符号)转义,以防止它们被视为HTML标记或引起解析问题。

在你的示例中," 编码成 "& 编码成 &,这是防止这些字符被解析为HTML标记的方法。

在ASP.NET MVC中,@Html.EditorFor 方法和 @Html.Raw 方法是处理HTML编码的两个不同方式。

  • @Html.EditorFor 方法通常用于生成输入元素,它会对属性值进行HTML编码,以防止XSS攻击。所以你看到的"&是这个方法自动进行的HTML编码。

  • @Html.Raw 方法用于在HTML视图中呈现原始HTML内容,它不会对内容进行HTML编码。所以你看到的"&是因为@Html.Raw(Model.Name)没有进行HTML编码,而直接将内容呈现出来。

浏览器本身也会处理HTML编码,以确保页面正常呈现,并且不会解释HTML实体作为标签。

在查看页面源码时,你可能会看到不同的编码方式,这取决于浏览器的渲染和解析方式。不同浏览器可能会以不同的方式呈现源代码。例如,< 被编码为 <,而 > 可能被呈现为 >,这是浏览器的自动处理。

总之,HTML实体编码用于防止特殊字符在HTML文档中引起问题,而ASP.NET MVC提供了不同的方式来处理HTML编码,具体取决于你的需求。

英文:

Given a model with the text \" &, this will render in the View in a textbox as " &. What internal mechanism is used to do this?

[edit, clarifying]

I have a model with this value

public string Name { get; set; } = "\" & <script></script>";

This renders in a textbox with

@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })

which renders on a view like so

<input ... value="" & <script></script>" aria-invalid="false">

(I removed the class, etc)

So the quote and ampersand were encoded but the script wasn't.

Similarly if you have this

<span>@Html.Raw(Model.Name)</span>

It becomes:

<span>" & <script></script></span>

I'm seeing this via edit as html in the developer tools.

My confusion is, what is encoding the ampersand. Is it Html.Raw, is it the browser itself?

Just to add to my confusion. If you use view source

The input box shows as

value="" & <script></script>"

the < is encoded but not the >

While the span shows as

<span>" &</span>

答案1

得分: 1

对于基于 Razor 的页面,可以使用 HtmlHelper.Raw

@Html.Raw(your_string_with_special_symbols);
英文:

For Razor based pages can use HtmlHelper.Raw:

@Html.Raw(your_string_with_special_symbols);

答案2

得分: 1

在一个asp.net应用程序中,编码是可配置的,通过web.config中的HttpRuntime.EncoderType设置。此设置的默认值取决于用于创建应用程序的Visual Studio版本。

这个设置(这是推荐的)会导致应用程序使用AntiXssEncoder来对所有内容进行编码。

<system.web>
    <httpRuntime encoderType="System.Web.Security.AntiXss.AntiXssEncoder,System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</system.web>

在内部,渲染方法(如HttpServerUtility中的方法)最终调用此类中的方法。

英文:

Encoding in an asp.net application is configurable, via the HttpRuntime.EncoderType setting in web.config. The default for this setting depends on the version of Visual Studio used to create the application.

This setting (which is recommended) causes your application to use the AntiXssEncoder to encode everything.

&lt;system.web.&gt;
    &lt;httpRuntime encoderType=&quot;System.Web.Security.AntiXss.AntiXssEncoder,System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&quot; /&gt;
&lt;/system.web&gt;

Under the covers, rendering methods (such as those in HttpServerUtility) end up calling the methods in this class.

huangapple
  • 本文由 发表于 2023年2月24日 15:35:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定