![]() Var uriDecoded = Uri.UnescapeDataString(uriEncoded) Ĭonsole.WriteLine(uriDecoded) // with space#fragmentĭifferences Between the Different Options Instead of UrlEncode() and UrlDecode(), the methods are called EscapeDataString() and UnescapeDataString(): var uriEncoded = Uri.EscapeDataString(url) Ĭonsole.WriteLine(uriEncoded) //http%3A%2F%%2Fresource%3Ffoo%3Dbar%20with%20space%23fragment Var webUtilityDecoded = WebUtility.UrlDecode(webUtilityEncoded) Ĭonsole.WriteLine(webUtilityDecoded) // with space#fragment How to Encode and Decode Using the Uri ClassĪlternatively, we can use the Uri class to encode and decode URLs. NET/C# content and get paid? > JOIN US! << var webUtilityEncoded = WebUtility.UrlEncode(url) Ĭonsole.WriteLine(webUtilityEncoded) //http%3A%2F%%2Fresource%3Ffoo%3Dbar+with+space%23fragment Wanna join Code Maze Team, help us produce more awesome. Usage is very similar to the previous examples, although there are no overloads: ![]() This class is in the Sytem.Net namespace. The documentation states that if we are not within a web application, we should use the WebUtility class to perform URL encoding and decoding instead. How to Encode and Decode Using the WebUtility Class There are also other method overloads to pass a Byte instead of a string type. By default, these methods use a UTF-8 encoding, but if this is not the case, there is an overload to pass a different encoding instead. These methods take a single string parameter containing the URL to be either encoded or decoded. Var httpUtilityDecoded = HttpUtility.UrlDecode(httpUtilityEncoded) Ĭonsole.WriteLine(httpUtilityDecoded) // with space#fragment Var httpUtilityEncoded = HttpUtility.UrlEncode(url) Ĭonsole.WriteLine(httpUtilityEncoded) //http%3a%2f%%2fresource%3ffoo%3dbar+with+space%23fragment The HttpUtility class, which is part of the System.Web namespace includes UrlEncode() and UrlDecode() methods: var url = with space#fragment" How to Encode and Decode URI Using the HttpUtility Class ![]() NET provides a few ways of encoding and decoding for us. This encoding is simple to perform, but instead of writing code to do this ourselves.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |