わかりました。' "
レンダリングされた html ページにアクセスすると、二重の html エンコーディングの問題が発生していることに気付きます。
状況を再現するには、 Replication:コードをコピーしてビューの 1 つに貼り付け、問題を自分で確認します。
HtmlString
MvcHtmlString
すでにエンコードされている html 文字列をエンコードすることは想定されていません。したがって、あなたの場合、
return new HtmlString(Encoder.HtmlEncode(resFile));
また
Sanitizer.GetSafeHtml(questionsAnswerString)
Html エンコードされた文字列を返します。その後、ビューで実際にもう一度エンコードしています。
これは、実際にコンテンツをレンダリングしているビューで、かみそりを使用しているために発生する可能性があります
@alreadyHtmlEncodedString
// razor's @ syntax html encodes the given string
//(irrespective of the fact that the given string is not html encoded
//or the given string is html encoded already or whatever.
//it just encodes the given string)
またはaspx
<%:alreadyHtmlEncodedString%>
// aspx's <%: %> html encodes the given string
//(irrespective of the fact that the given string is not html encoded
//or the given string is html encoded already or whatever.
//it just encodes the given string)
だから、もしそうなら。すでに html エンコードされている文字列には Html.Raw を使用します。または、安全でない非 html エンコード文字列については、razor の @ 構文に依存するだけです。
以下は、シナリオを再現するためのコードです。そして、サンプル出力と画像。ビューの 1 つに以下のコードを挿入します。
@{string quotes = @"'""";
string quotesHtmlEncoded = Html.Encode(@"'""");
string hebrew = @"like רמב""ם or צ'ק";
string hebrewHtmlEncoded = Html.Encode(@"like רמב""ם or צ'ק");
string sampleXss = "<script>alert('1')</script>";
string sampleXssHtmlEncoded = Html.Encode("<script>alert('1')</script>");
}
<table border="1">
<thead>
<tr>
<th></th>
<th>razor @@
</th>
<th>Raw
</th>
<th>MvcHtmlString.Create
</th>
</tr>
</thead>
<tbody>
<tr>
<td>quotes
</td>
<td>
@quotes
</td>
<td>
@Html.Raw(quotes)
</td>
<td>
@MvcHtmlString.Create(quotes)
</td>
</tr>
<tr>
<td>quotesHtmlEncoded
</td>
<td>
@quotesHtmlEncoded
</td>
<td>
@Html.Raw(quotesHtmlEncoded)
</td>
<td>
@MvcHtmlString.Create(quotesHtmlEncoded)
</td>
</tr>
<tr>
<td>hebrew
</td>
<td>
@hebrew
</td>
<td>
@Html.Raw(hebrew)
</td>
<td>
@MvcHtmlString.Create(hebrew)
</td>
</tr>
<tr>
<td>hebrewHtmlEncoded
</td>
<td>
@hebrewHtmlEncoded
</td>
<td>
@Html.Raw(hebrewHtmlEncoded)
</td>
<td>
@MvcHtmlString.Create(hebrewHtmlEncoded)
</td>
</tr>
<tr>
<td>sampleXss
</td>
<td>
@sampleXss
</td>
<td>
@Html.Raw(sampleXss)
</td>
<td>
@MvcHtmlString.Create(sampleXss)
</td>
</tr>
<tr>
<td>sampleXssHtmlEncoded
</td>
<td>
@sampleXssHtmlEncoded
</td>
<td>
@Html.Raw(sampleXssHtmlEncoded)
</td>
<td>
@MvcHtmlString.Create(sampleXssHtmlEncoded)
</td>
</tr>
</tbody>
</table>
サンプル出力イメージ
.