Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Html.Raw(Model.MyHtml)
- public ActionResult Save(MyModel model)
- {
- if (HasScripts(model.MyHtml)
- {
- ModelState.AddModelError("MyHtml", "The html cannot contain script tags");
- }
- if (!ModelState.IsValid)
- {
- return View(model);
- }
- // save and redirect
- }
- public bool HasScripts(string html)
- {
- HtmlDocument document = new HtmlDocument();
- document.LoadHtml(html);
- HtmlNode root = document.DocumentNode;
- return root.Descendants("script").Any();
- }
- public string RemoveScripts(string html)
- {
- HtmlDocument document = new HtmlDocument();
- document.LoadHtml(html);
- HtmlNode root = document.DocumentNode;
- IEnumerable<HtmlNode> scripts = root.Descendants("script");
- for(int i = 0; i < scripts.Count(); i++)
- {
- HtmlNode script = scripts[i];
- script.Remove();
- }
- return scripts.Any() ? document.ToString() : html;
- }
- model.MyHtml = RemoveScripts(model.MyHtml);
Add Comment
Please, Sign In to add comment