Url Encode In Asp.net

Url Encode In Asp.net Url Encode in Asp.net

Before passing the parameters to web server, make sure they are encoded. Some browsers encode them internally but to be at safe side, encode them to avoid termination of the request. Here we will use HttpUtility.UrlEncode function to encode the parameters.

using System;
using System.Web;
public partial class URLEncodeDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string userName = "Satalaj More.";
string url = "http://www.bing.com?";
string parameter = "q=" + HttpUtility.UrlEncode(userName);
url = url + parameter;
Response.Redirect(url);
}
}

Note: At server side you don't need to decode the URL parameters.

Example above uses Bing open search. The out put of the above program shows bing search engine results for keyword .

You may be settings dynamic values to URL parameter. In this case you must ensure that the parameter values properly encoded using HttpUtility.UrlEncode.

Tags:

Url Encode In Asp.netAsp.net Urldecode QueryString[Asp.net] Get current Page URL absolute URI and query string parameters.How to Pass To Query String In Asp.netAsp.net use Fully Qualified RedirectUrlASP.net File HandlingRequest Urlasp net image uploadAsp.net Interview Questions for 2 years or 3 years experianceASp.net 2.0 step by step Membership Provider Asp.net How to display HTML contents

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.