Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the equivalent of HttpUtility.JavaScriptStringEncode in .Net Core 1.1?

Tags:

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.

like image 210
Matt R Avatar asked Mar 11 '17 06:03

Matt R


People also ask

What is HttpUtility Javascriptstringencode?

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.

What is HttpUtility 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.


1 Answers

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"); 
like image 187
RavingDev Avatar answered Sep 24 '22 01:09

RavingDev