Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plus sign in query string for ASP.NET site

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?

like image 302
John Collins Avatar asked Dec 21 '11 20:12

John Collins


People also ask

How do you enter a plus sign in URL?

URL Encoding (Percent Encoding) URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

Can we pass in query string?

To pass in parameter values, simply append them to the query string at the end of the base URL.

How do you pass + in URL query string?

string msg = "<t1> +2"; string encoded = Server. UrlEncode(Server. HtmlEncode(msg));


1 Answers

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.

like image 164
Tomislav Markovski Avatar answered Oct 23 '22 08:10

Tomislav Markovski