我在我的客户调查系统中使用AngularJS
和ASP.Net Web API 2
。
我的网站使用wysiwyg
组件来帮助用户输入他/她的数据。这是我正在使用的wysiwyg
生成的html:
<h2 style="text-align: center;"><span style="text-decoration: underline;"><strong>Click here</strong></span><strong> to start using the </strong><strong><em>HTML editor online</em></strong></h2>
<p><span style="font-weight: 400;">Please</span><span style="font-weight: 400;"> try out the WYSIWYG HTML editor features found in the kitchen sink above to edit and format your text and images</span></p>
<p><span style="font-weight: 400;">You’ll see the content created in the WYSIWYG-HTML editor in source code format on the right.</span><br /><br /></p>
<p> </p>
<p><img onclick="alert('Hello world')" onmouseout="alert('Mouse out')" src="https://icon2.kisspng.com/20180416/ucw/kisspng-tanki-online-video-gaming-clan-world-of-tanks-avat-joker-5ad4b1028d0565.2188790515238883865776.jpg" alt="" width="260" height="260" /></p>
这就是我在API上为上面的html代码编码所做的事情。
/// Encode submited html content
/// </summary>
/// <returns></returns>
[Route("encode")]
[HttpPost]
public IHttpActionResult Encode([FromBody] EncodeViewModel httpContent)
{
if (httpContent == null)
{
httpContent = new EncodeViewModel();
Validate(httpContent);
}
if (!ModelState.IsValid)
return BadRequest(ModelState);
var encodedHtmlContent= HttpUtility.HtmlEncode(httpContent.HtmlContent);
// Save content to database here.
return Ok(encodedHtmlContent);
}
当我的前端客户端加载编码的html并显示到网站页面时。我希望当我单击图像时,不会显示消息Hello world或Mouse out。
这意味着,我只允许显示文本、图像和视频,但不允许在内容中使用内联代码。
对此有什么解决方案吗?
谢谢,
https://stackoverflow.com/questions/51581808
复制相似问题