Guest User

Untitled

a guest
May 24th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. @Html.Raw(Model.MyHtml)
  2.  
  3. public ActionResult Save(MyModel model)
  4. {
  5. if (HasScripts(model.MyHtml)
  6. {
  7. ModelState.AddModelError("MyHtml", "The html cannot contain script tags");
  8. }
  9. if (!ModelState.IsValid)
  10. {
  11. return View(model);
  12. }
  13. // save and redirect
  14. }
  15.  
  16. public bool HasScripts(string html)
  17. {
  18. HtmlDocument document = new HtmlDocument();
  19. document.LoadHtml(html);
  20. HtmlNode root = document.DocumentNode;
  21. return root.Descendants("script").Any();
  22. }
  23.  
  24. public string RemoveScripts(string html)
  25. {
  26. HtmlDocument document = new HtmlDocument();
  27. document.LoadHtml(html);
  28. HtmlNode root = document.DocumentNode;
  29. IEnumerable<HtmlNode> scripts = root.Descendants("script");
  30. for(int i = 0; i < scripts.Count(); i++)
  31. {
  32. HtmlNode script = scripts[i];
  33. script.Remove();
  34. }
  35. return scripts.Any() ? document.ToString() : html;
  36. }
  37.  
  38. model.MyHtml = RemoveScripts(model.MyHtml);
Add Comment
Please, Sign In to add comment