Friday, August 8, 2014

Encoding URL in ASP.NET using C#


Some time in the URL we get some spacial characters like plus(+) is not getting in code behind. As example in an encrypted URL like
http://localhost:1033/page.aspx?Value=1+E4Hccj9hE=
Here the value of l is "1+E4Hccj9hE=" but when you will get the value by Request.QueryString() it will get the value "1 E4Hccj9hE=". So it will occur an error in your project. 

So how to prevent this?

There is a method called Server.UrlEncode. Using this method we have to pass the url to execute. Then all the spacial
characters will be encoded into their own code. Like for space it will be %20 in the URL.

The C# code is..
protected void Page_Load(object sender, EventArgs e)
{
    Response.Redirect("~/page.aspx?Value=" + Server.UrlEncode("+12+e"));
}

Try this and save your day or some one others.

0 comments:

Post a Comment

Popular Posts

Pageviews