以前在后台拼接的HTML到前台,总是有问题,要么显示不出正确的样式,要么直接显示编码,就只能自己在程序里面自己做转换。
今天看到htmlHelper.Raw()函数,可以满足这个功能。
官方解释是这样的:返回不是 HTML 编码的标记(This method wraps HTML markup using the IHtmlString class, which renders unencoded HTML),
还有个更加详尽的解释:The Razor syntax @ operator HTML-encodes text before rendering it to the HTTP response. This causes the text to be displayed as regular text in the web page instead of being interpreted as HTML markup.
Use the Raw method when the specified text represents an actual HTML fragment that should not be encoded and that you want to render as markup to the HTTP response.
意思是说,在响应http时,razor语法将html标签编码后返回,请求到前台后,html标签就被当做普通文本展现,而不是被当做html标签来渲染。后面一句提供了解决办法:响应时,使用未编码的html标签。