问题
When the characters %20 appears in between paramaters a url, my MVC routing stops considering that a string.
Why is that, and how can I approach handling "%20" characters in my URL?
Example URL
http://localhost:40494/ListContents/Delete/asdf%20/5430f394...
public ActionResult Delete(string DNSName, Guid id)
{...}
routes.MapRoute(
"Delete", // Route name
"ListContents/Delete/{DNSName}/{id}", // URL with parameters
new { controller = "ListContents", action = "Delete" } // Parameter defaults
);
However Both the following URLs work fine
http://localhost:40494/ListContents/Delete/asdf%20SOMETHING_HERE/5430f394...
http://localhost:40494/ListContents/Delete/%20asdf/5430f394-946c-4f82-ac13-9d5efafe9127
回答1:
If an empty space is at the end of any section of the URL before the next slash, it throws a HttpException in the System.Web.Util.FileUtil.CheckSuspiciousPhysicalPath() method which is handled by MVC and you'll get a HTTP 404 response.
You can verify that yourself by checking the checkbox for Throw in:
Visual StudioDebugExceptionsCommon Language Runtime Exceptions
Generally you should not have empty spaces in your URLs. I personally format my urls, that all spaces becomes a dash (-).
回答2:
I think the problem is that in the example where it doesn't work is because it can't be parsed as a valid URL, it will be read as
http://localhost:40494/ListContents/Delete/asdf /5430f394...
Instead, you would be safe to just remove the %20 from that url safely.
回答3:
Check if the id field of the table isn't a string (nchar(x)). If so, check if the respective id has the exact lenghth defined in the type declaration. If not (if it has less chars), that's the problem (it should have the EXACT lenghth you declared). This worked for me.
来源:https://stackoverflow.com/questions/4853626/20-followed-by-slash-followed-by-more-data-causes-asp-net-mvc-3-routing-to-fail