How can I submit an input of text type or a textarea which has HTML tags using .NET Blazor server-side approach?
When I submit an input of text type without HTML tags, the information submitted enters perfectly, but when I include some HTML tags in the input, the submitted text comes empty, how can I submit inputs with HTML tags?
Here is my HTML code:
<input type="text"
id="quill-result-body"
class="form-control"
required
@bind="BlogPost!.Body" />
As I mentioned, if the input does not have HTML tags, the submitted information located in BlogPost.Body comes perfect, but when I put some HTML tags, it comes empty.
Anyone can help me? Thanks in advance
2
Answers
It is because by default implementation
Blazor
tries to prevent security attacks / issues. Here in this case directly taking input forHTML
content may lead to Cross Site Scripting Attacks (XSS). Therefore, you get empty input value.But if there is requirement of HTML content to be displayed and is absolutely necessary. You may use the
HtmlString
type.You need to be more careful in handling and sanitizing the input properly. Though it is not a good practice to be taking HTML as input.Here is the code of new page that works for me – just created new Blazor Server to try.
And here’s HTML input and Console output.
What Blazor .NET version do you use?