views:

587

answers:

2

On my site, an encoded quote (%22) in url path causes "Illegal characters in path" error

I want specify search URLs like so:

www.site.com/search/%22Vitamin+C%22

%22 is an encoded quotation mark "

I'm using a Asp.Net URL Routing and the route is specified like this: "search/{searchTerm}"

When Context["searchTerm"] is retrieved and Decoded, it would result in the above example being: "Vitamin+C" [including quotes]

It would seem that Asp.Net thinks that the there are illegal characters in the URL. I don't understand why, when I AM URLEncoding the text.

How can I encode Quotation marks without Asp.Net complaining? Many people will use quotation marks to group words together and I want to allow this "smart searching"

+1  A: 

For free-form search terms, you should use a QueryString parameter instead of a URL chunk. If you have structured searches (a limited list of categories with names that conform to valid URL requirements or search by zip codes) then you could use the URL structure you are using without issue. You can encode and put whatever you want, including quotes, in the querystring parameter.

John Sheehan
Thanks! I found an example of search implemented here: http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
Atømix
A: 

Check out the HttpServerUtility.UrlEncode Method

Rob
Yeah, I am using that method. That's why I'm perplexed. Because I'm using Routing, ASP.Net doesn't like "Quotes" in the path.
Atømix