Thursday, July 2, 2015

Escaping string variables in C#

I was recently working on an ASP.NET Web API project where we would be accepting incoming strings from non .NET clients.

Therefore, we did not necessarily know the format of the incoming strings, therefore, we would have to ensure that all of the string input was valid for JSON serialization before performing the serialization.

Therefore, we were looking for a way to escape any incoming strings that were assigned to C# variables.

Well, fortunately, there is a very easy way to accomplish this using the Regex.Escape method!!  https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.escape%28v=vs.110%29.aspx

As you can probably guess, I used this method on my incoming strings and I was able to escape my strings for successful JSON serialization!!

No comments:

Post a Comment