I have found myself in need of scrubbing javascript out of comments being added by users in a .Net Core MVC application. In previous frameworks, this could be achieved by first passing your string into JavaScriptStringEncode.
var comment = HttpUtility.JavaScriptStringEncode(model.Comment);
However, I haven't been able to find the equivalent in .net core.
HttpUtility.UrlDecode Method (System.Web)Converts a string that has been encoded for transmission in a URL into a decoded string. To encode or decode values outside of a web application, use the WebUtility class.
The HttpUtility class is used internally by the HttpServerUtility class, whose methods and properties are exposed through the intrinsic ASP.NET Server object. Additionally, the HttpUtility class contains encoding and decoding utility methods that are not accessible from the Server.
Here is an equivalent of HttpUtility.JavaScriptStringEncode
in .net core:
using System.Text.Encodings.Web; //part of System.Text.Encodings.Web nuget package ... var encodedText = JavaScriptEncoder.Default.Encode("TextToEncode");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With