Home > DeveloperSection > Blogs > Encode and Decode in .Net

Encode and Decode in .Net

C# C# 
0 Comment(s)
 4019  View(s)
Rate this:
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. However, 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 to another.
·  Access the URL or pass the URL.
·  Fetch the data or insert data in Database.
·  Read the xml data or insert data in xml.
·  Change the special character from string
·  And security purpose etc.

In ASP.Net we used the following encode and decode method are
·  Server.HtmlEncode()
·  Server.HtmlDecode()
·  Server.HtmlUrlEncode()
·  Server.HtmlUrlDecode ()
·  HttpUtility.UrlEncode()
·  HttpUtility.UrlDecode ()

string strData="?data??"
Response.Write(Server.HtmlEncode(strData)); //Encode the value here

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

URL is: http://localhost:49714/WebSite8/Default.aspx
Encoded URl is: http%3a%2f%2flocalhost%3a49714%2fWebSite8%2fDefault.aspx

Don't want to miss updates? Please click the below button!

Follow MindStick