Search code examples
c#asp.neturlrouteswebforms

A potentially dangerous Request.Path value was detected from the client (*)


I am receiving the rather self explanatory error:

A potentially dangerous Request.Path value was detected from the client (*).

The issue is due to * in the request URL:

https://stackoverflow.com/Search/test*/0/1/10/1

This url is used to populate a search page where 'test*' is the search term and the rest of the url relates to various other filters.

Is there an easy way to allow these special characters in the URL? I've tried modifying the web.config, to no avail.

Should I manually encode / decode the special characters? Or is there a best practice for doing this, I would like to avoid using query strings. - but it may be an option.

The application itself is a c# asp.net webforms application that uses routing to produce the nice URL above.


Solution

  • The * character is not allowed in the path of the URL, but there is no problem using it in the query string:

    http://localhost:3286/Search/?q=test*
    

    It's not an encoding issue, the * character has no special meaning in an URL, so it doesn't matter if you URL encode it or not. You would need to encode it using a different scheme, and then decode it.

    For example using an arbitrary character as escape character:

    query = query.Replace("x", "xxx").Replace("y", "xxy").Replace("*", "xyy");
    

    And decoding:

    query = query.Replace("xyy", "*").Replace("xxy", "y").Replace("xxx", "x");