% 20, and then a slash followed by more data causes ASP.NET MVC 3 routing to fail?

When% 20 characters appear between url parameters, my MVC routing stops reading the string.

Why is this, and how can I approach the processing of the characters "% 20" in my URL?

URL example

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 of the following URLs are working fine

 http://localhost:40494/ListContents/Delete/asdf%20SOMETHING_HERE/5430f394... http://localhost:40494/ListContents/Delete/%20asdf/5430f394-946c-4f82-ac13-9d5efafe9127 
+3
source share
3 answers

If the blank space is at the end of any section of the URL before the next slash, it throws an HttpException method in the System.Web.Util.FileUtil.CheckSuspiciousPhysicalPath() method, which is processed by MVC, and you will receive an HTTP 404 response.

You can verify this yourself by checking the box for Throw in:

  • Visual Studio
  • Debug
  • Exceptions
  • Common Language Runtime Exceptions

As a rule, you should not have empty spaces in your URLs. I personally format my URLs so that all spaces become dashes (-).

+3
source

I think the problem is that in the example where it does not work, this is because it cannot be parsed as a valid URL, it will be read as

http://localhost:40494/ListContents/Delete/asdf /5430f394...

Instead, it will be safe for you to simply remove %20 from this URL.

0
source

Check if the table id field is not a string (nchar (x)). If so, check to see if the corresponding identifier has the exact length specified in the type declaration. If not (if it has fewer characters), then this is the problem (it should have the EXACT length you declared). It worked for me.

0
source

All Articles