301 Rewrite Rules can break MVC forms

Posted on Monday, May 12, 2014 at 3:19 PM into code, csharp & gotcha by Steve Woods.
Roughly a 1 minute read.

If you use a 301 rewrite rule in IIS to ensure all of your URLs are lowercase (perhaps for SEO purposes), beware that you may end up with broken forms in your application.

Sometimes when I'm building an app in .NET MVC, I end up accidentally using CamelCase in my URLs (because I'm a very bad programmer). I might end up with a form declaration which looks like this:


 
...

This is because the Controllers themselves in the MVC project conform to the CamelCase pattern, i.e. ProcessingController ... 

If you're using IIS Express or similar when developing locally, you probably won't notice - it's only when you deploy to the Live environment that the poo will start to fly, because that is where the ReWrite rules exist.

The reason there is a problem is because the rewrite rule returns a GET request each time it's called. You may be calling /Processing/MyAction as a POST, but when it gets rewritten by the server, the form will actually attempt to post to /processing/myaction as a GET request. And your app will break.

The solution?

Ensure all of the request URLs in your application are entirely lowercase - in HTML, Javascript. CSS. This will stop the Rewrite Rule from firing, and your POST request will survive.

;

Comments

Please consider what you post!
Chillax if you're angry.