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 ()
Example1:
string strData="?data??";
Response.Write(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
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 ()
Example1:
string strData="?data??";
Response.Write(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
Anonymous User
09-Mar-2019Thank You.
Sunil Singh
16-Jun-2017I always seek to read your articles.