Plus sign in query string for ASP.NET site

John Collins picture John Collins · Dec 21, 2011 · Viewed 7.7k times · Source

I few years ago I created a database driven ASP.NET site, which uses a single APSX page to display all site pages. So all the URLs of the site are in the following format:

/main.aspx?page=Page+Title+One

/main.aspx?page=Another+Article+Title

/main.aspx?page=Third+Page

The main.aspx page gets the query string data (Page+Title+One for example) and uses it as a key to pull the appropriate article content from the SQL server database. The actual title of the page is stored in the db with spaces instead of pluses (for example "Page Title One").

The poor decision to go with the + sign as a word separator in the URL query string is causing lots of issues with search engines lately (duplicate content, etc.), so I want to fix it, but without changing URLs.

What I want to do is when search engine or visitor tries to visit the wrong URL missing the + signs and having white spaces instead, for example:

/main.aspx?page=Page Title One

I want to do 301 permanent redirect to:

/main.aspx?page=Page+Title+One

To be able to do this I need to check if the query string value has pluses or white spaces, however when I get the value with Request.QueryString["page"] even if the actual quesry string has pluses in it I still get string with white spaces "Page Title One".

The site runs on IIS6/Win 2003.

How can I do this?

Answer

Tomislav Markovski picture Tomislav Markovski · Dec 21, 2011

Using Request["key"], it automatically converts all "+" signs into spaces. You need to use Request.RawUrl to see if there are plus signs.

Additionally, you can use System.Web.HttpUtility.ParseQueryString to parse any string query. You can just test if Request.QueryString.ToString().Contains("+") is true, and do logic from there.