How to UrlEncode Plus Sign "+" in QueryString and UrlDecode Plus Symbol in ASP.NET

by Updated June 23, 2010

Here's how you can UrlEncode the plus sign (+) in a URL querystring in ASP.NET and then retrieve the plus symbol after UrlDecoding the string.

In this example, I will do a postback and redirect the Server.UrlEncoded string to another page.  First we will replace the "+" sign with "%252b" and then Server.UrlEncode the text:

protected void PostButton_Click(object sender, EventArgs e)
{
        // replace plus sign "+" with "%252b"
        string myTitle = TitleTextBox.Text.Trim().Replace("+", "%252b"); 
        Response.Redirect("~/default.aspx?title=" + Server.UrlEncode(myTitle));
}

Now on the page we redirected to, we will Server.UrlDecode the QueryString and replace "%2b" with the "+" plus symbol:

protected void Page_Load(object sender, EventArgs e)
{
       if (!Page.IsPostBack)
        {

            // Get Querystring for Title, if it exists
            string pageTitle = Server.UrlDecode(Request.QueryString["title"]);
           
            if (!string.IsNullOrEmpty(pageTitle))
            {
  // replace encoded plus sign "%2b" with real plus sign +
  pageTitle = Regex.Replace(pageTitle, "%2b", "+", RegexOptions.IgnoreCase);
  PageTitleTextBox.Text = pageTitle;         
            }
        }
}

So that's a simple technique that you can use to Server.UrlEncode a string that contains the Plus sign and then retrieve the plus symbol after the querystring has been decoded using Server.UrlDecode in ASP.NET and C#.

 


0
3

3 Comments

anonymous by webmonkey on 2/13/2012
i did not want the plus sign in the subject of an email. so

ename = Server.UrlEncode(ename).Trim().Replace("+", "%20")
anonymous by Ravi on 10/5/2012
Solved my problem by using replace method
Thanks,
Ravi
anonymous by Shashank on 12/19/2016

Awesome Solution. That worked for me in Java also

Add your comment

by Anonymous - Already have an account? Login now!
Your Name:

Comment:
Enter the text you see in the image below
What do you see?
Can't read the image? View a new one.
Your comment will appear after being approved.

Related Posts


Here's how to install Internet Information Services (IIS7) on a Windows 7 (or Vista) computer so that ASP.NET websites will run on the IIS7 web server. First, you will want to make sure that you are signed into an account with Administrator access on your...  more »

Adding a CSS border to an ASP.NET Image control was a mystery to me for the longest time. While you could easily use an html image and add the runat="server" to it and then add CSS, I really wanted to use an asp:Image control along with a CSS border....  more »

After running a ASP.NET website on IIS 7.5 for the first time on a Windows 7 computer, I was faced with the following error message: Login failed for user 'IIS APPPOOL\ASP.NET v4.0'. Description: An unhandled exception occurred during the execution of the...  more »

This is one of those simple web page design things that can drive a web developer absolutely crazy.  more »

If you do any sort of ASP.NET programming there usually comes a time when you need to get a websites Base URL. The following shows two examples, the first example shows how to get the Base Site Url using C#, which can be used for getting both the...  more »