I use the Html.Raw to print a raw html content, for example when I send some thing like ViewBag.div = "<div> Hello </div>";
from the controller to the view side it does not print a raw html content unless I use the Html.Raw
method but if I have an encoded content, like content encoded using jquery and inserted into the database and I want to print it as a raw html content the Html.Raw
method does not work and I have to use HttpUtility.HtmlDecode(EncodedContent)
before I use Html.Raw
so please could anyone explain why it acts in this way and what is the proper situation to use Html.Raw
method? or in another way, why Html.Raw
does not work when it receives html entities as a parameter instead of html tags?.
相关问题
- Carriage Return (ASCII chr 13) is missing from tex
- MVC-Routing,Why i can not ignore defaults,The matc
- How to store image outside of the website's ro
- 'System.Threading.ThreadAbortException' in
- Request.PathInfo issues and XSS attacks
相关文章
- asp.net HiddenField控件扩展问题
- asp.net HiddenField控件扩展问题
- Asp.Net网站无法写入错误日志,测试站点可以,正是站点不行
- asp.net mvc 重定向到vue hash字符串丢失
- FormsAuthenticationTicket expires too soon
- “Dynamic operations can only be performed in homog
- What is the best way to create a lock from a web a
- Add to htmlAttributes for custom ActionLink helper
Because encoded characters are HTML, and the Raw version of that string is the encoded one.
Html.Raw renders what it is given without doing any html encoding, so with
ViewBag.div = "<div> Hello </div>";
:Renders
However, when you have encoded characters in there, such as
ViewBag.Something = ">";
the raw version of that is>
. To get back to actual html you need toHtml.Raw(HttpUtility.HtmlDecode(EncodedContent));
as you've said.If Html.Raw did do the decoding then it would be confusing, and we would need something that didn't do it. ;-)
Html.Raw
Method asks the Razor Engine to do not encode the special chars.Razor Engine Encodes the special chars because it considers that you want to show them in the state you sent to it so it encodes the special chars and the browser decodes them again to show you them in the original state (the state that you sent to the razor engine), but if you use the
Html.Raw
that means that you ask the Razor engine to do not encode the special chars of your content and actually that does not mean that you ask it to decode your encoded content such the content you get from the database, it just ask the engine to do not encode the special chars so if you have an encoded content in the database you have to decode it using HttpUtility.HtmlDecode and then to ask the razor engine to do not encode the returned html tags by usingHtml.Raw
.For eaxmple if you have this content in your database
now if you print it without using
HTML.Raw
the razor engine will encode the special chars in that content to be printed in the browser as it is but if you useHTML.Raw
that means to do not do anything over the content so the browser will render them as a set of html tags which have a content inside it but not a formatted data, you will get some thing like:but if you use
Html.Raw(HttpUtility.HtmlDecode(EncodedContent))
then you will get a formatted data in your page like the following content because the content sent to the browser is html tags not entitiesdklxf;kldk;dlkdxl'f;dlxd'fdlf;ldk;dlkf