A potentially dangerous Request.Path value was det

2018-12-31 16:21发布

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.

7条回答
妖精总统
2楼-- · 2018-12-31 17:01

If you're using .NET 4.0 you should be able to allow these urls via the web.config

<system.web>
    <httpRuntime requestPathInvalidCharacters="&lt;,&gt;,%,&amp;,:,\,?" />
</system.web>

Note, I've just removed the asterisk (*), the original default string is:

<httpRuntime requestPathInvalidCharacters="&lt;,&gt;,*,%,&amp;,:,\,?" />

See this question for more details.

查看更多
ら面具成の殇う
3楼-- · 2018-12-31 17:07

When dealing with Uniform Resource Locator(URL) s there are certain syntax standards, in this particular situation we are dealing with Reserved Characters.

As up to RFC 3986, Reserved Characters may (or may not) be defined as delimiters by the generic syntax, by each scheme-specific syntax, or by the implementation-specific syntax of a URI's dereferencing algorithm; And asterisk(*) is a Reserved Character.

The best practice is to use Unreserved Characters in URLs or you can try encoding it using java.net.URLEncoder.

Keep digging :

查看更多
公子世无双
4楼-- · 2018-12-31 17:09

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");
查看更多
倾城一夜雪
5楼-- · 2018-12-31 17:09

This exception occurred in my application and was rather misleading.

It was thrown when I was calling an .aspx page Web Method using an ajax method call, passing a JSON array object. The Web Page method signature contained an array of a strongly-typed .NET object, OrderDetails. The Actual_Qty property was defined as an int, and the JSON object Actual_Qty property contained "4 " (extra space character). After removing the extra space, the conversion was made possible, the Web Page method was successfully reached by the ajax call.

查看更多
骚的不知所云
6楼-- · 2018-12-31 17:17

You should encode the route value and then (if required) decode the value before searching.

查看更多
十年一品温如言
7楼-- · 2018-12-31 17:17

Try to set web project's server propery as Local IIS if it is IIS Express. Be sure if project url is right and create virual directory.

查看更多
登录 后发表回答