---
title: "Encode and Decode in .Net"  
description: "Html Encode method encodes a particular string to be displayed in a browser. It is important to encode strings prior it’s rendering in the page, main"  
author: "Amit Singh"  
published: 2010-11-12  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/51/encode-and-decode-in-dot-net  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Encode and Decode in .Net

Html Encode method encodes a particular string to be displayed in a browser. It is important to encode strings prior it’s rendering in the page, mainly to avoid cross-site script injection (XSS) and HTML [injection attacks](https://www.mindstick.com/forum/160895/protect-sql-server-database-against-sql-injection-attacks). However, [developers](https://www.mindstick.com/blog/302688/handling-errors-in-rust-a-comprehensive-guide-for-developers) so often forget to call the encode function.\
Html Decode method decodes a particular string which is encoded.\
\
Encoding and decoding is needed in somewhere like \
· Pass the values from [one page](https://www.mindstick.com/forum/118/problem-in-display-content-of-one-page-to-another-page) to another.\
· Access the URL or pass the URL.\
· Fetch the data or [insert data in Database](https://www.mindstick.com/forum/33723/insert-data-in-database-using-windows-application-c-sharp).\
· Read the [xml data](https://www.mindstick.com/forum/149/problem-in-showing-the-xml-data-in-table-form-on-browser) or insert data in xml.\
· Change the [special character](https://www.mindstick.com/forum/119/special-character-remove-from-url-or-any-string) from string \
· And security purpose etc.\
\
In ASP.Net we used the following [encode and decode](https://www.mindstick.com/forum/159053/how-do-i-encode-and-decode-a-base64-string-in-c-sharp) method are\
· Server.HtmlEncode()\
· Server.HtmlDecode()\
· Server.HtmlUrlEncode() \
· Server.HtmlUrlDecode ()\
· HttpUtility.UrlEncode()\
· HttpUtility.UrlDecode ()\
\
Example1:\
string strData="?data??"; \
[Response.Write](https://www.mindstick.com/forum/1031/response-write-doesn-t-display-image)(Server.HtmlEncode(strData)); //Encode the value here\
\
Example2:\
string strUrl=Request.Url.ToString();//Request.Url get the url \
Response.Write("<font color='green'>URL is:</font> " + strUrl +"<br/>");\
Response.Write("<font color='green'>Encoded URL is: </font>" + HttpUtility.UrlEncode(strUrl));//Encoded Url Here\
\
Output:\
URL is: http://localhost:49714/WebSite8/Default.aspx\
Encoded URl is: http%3a%2f%2flocalhost%3a49714%2fWebSite8%2fDefault.aspx \
\

---

Original Source: https://www.mindstick.com/blog/51/encode-and-decode-in-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
