---
title: "Encoding and Decoding in ASP.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, mai"  
author: "Amit Singh"  
published: 2010-11-27  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/203/encoding-and-decoding-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# Encoding and Decoding in ASP.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. 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:

- Server.HtmlEncode()
- Server.HtmlDecode()
- Server.HtmlUrlEncode()
- Server.HtmlUrlDecode ()
- HttpUtility.UrlEncode()
- HttpUtility.UrlDecode ()

##### How Use these method in ASP.Net

##### Server.HtmlEncode():\
Syntax:

\

![Encoding and Decoding in ASP.Net](https://www.mindstick.com/mindstickarticle/b3d1fb4c-f386-49af-a464-fc96467eb957/images/9c089028-9c07-4b04-b202-b1f2fbb40aee.png)

And

![Encoding and Decoding in ASP.Net](https://www.mindstick.com/mindstickarticle/b3d1fb4c-f386-49af-a464-fc96467eb957/images/c3ca006a-dc5d-477b-957b-f21d38232d5e.png)

##### Example:

string strData="?data??";

Response.Write(Server.HtmlEncode(strData)); //Encode the value here

##### Server.HtmlDecode()

##### Syntax:

![Encoding and Decoding in ASP.Net](https://www.mindstick.com/mindstickarticle/b3d1fb4c-f386-49af-a464-fc96467eb957/images/bcbeb6ce-d3e6-4edf-93fc-f8560bacd1c0.png)

And

![Encoding and Decoding in ASP.Net](https://www.mindstick.com/mindstickarticle/b3d1fb4c-f386-49af-a464-fc96467eb957/images/d64e2a1f-8107-4d17-ba91-2df547ae80b4.png)

##### Example:

string strData="?data??";

Response.Write(Server.HtmlDecode(strData)); //Decode the value here

##### Server.HtmlUrlEncode()

#####

This function is used for encode the URL

## Syntax:

![Encoding and Decoding in ASP.Net](https://www.mindstick.com/mindstickarticle/b3d1fb4c-f386-49af-a464-fc96467eb957/images/c28d1442-896a-4c5c-bf9f-7caf775a2079.png)

We pass the other parameter as:

string HttpUtility.UrlEncode(byte[] bytes), string HttpUtility.UrlEncode(byte[] bytes,System.Text.Encoding e)and string HttpUtility.UrlEncode(byte[] bytes,int offset,int count)

##### Example:

```
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:

![Encoding and Decoding in ASP.Net](https://www.mindstick.com/mindstickarticle/b3d1fb4c-f386-49af-a464-fc96467eb957/images/c7bdb1c3-1db1-4e63-a79d-318f0d215938.png)

Server.HtmlUrlDecode ()

This function is used for decode the encoded URL

##### Syntax:

![Encoding and Decoding in ASP.Net](https://www.mindstick.com/mindstickarticle/b3d1fb4c-f386-49af-a464-fc96467eb957/images/4f5ada9e-e2c5-4cf4-b738-40a1d2077591.png)

We pass the other parameter as:

string HttpUtility.UrlDecode(byte[] bytes), string HttpUtility.UrlDecode(byte[] bytes,System.Text.Encoding e)and string HttpUtility.UrlDecode(byte[] bytes,int offset,int count)

##### Example:

```
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) + "<br/>");//Encoded Url HereResponse.Write("<font color='green'>Decoded URL is: </font>" + HttpUtility.UrlDecode(HttpUtility.UrlEncode(strUrl)));//Decoded Url Here
```

Output:

![Encoding and Decoding in ASP.Net](https://www.mindstick.com/mindstickarticle/b3d1fb4c-f386-49af-a464-fc96467eb957/images/501535ba-9ee1-4d4a-9627-d2823573af9d.png)

HttpUtility.UrlEncode() and HttpUtility.UrlDecode ()

Both method work same as to Server.HtmlUrlEncode() and Server.HtmlUrlDecode() respectively.

Note:

In Latest version ASP.Net 4.0, we write the code more concise way

##### Example:

<p><%=Server.HtmlEncode(strData); %> or

<%=HttpUtility.UrlEncode(strData); %></p>

But in **Asp.net 4.0** we use as <%: strData %>, it is similar to both

This is the simple concept on encoding and decoding in ASP.Net.

\

---

Original Source: https://www.mindstick.com/articles/203/encoding-and-decoding-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
