ASP.NET MVC : strange POST behavior
ASP.NET MVC 2 app
I have two actions on my controller (Toons):
- [GET] List
- [POST] Add
App is running on IIS7 integration mode, so /Toons/List works fine. But when I do POST (that redirects to /Toons/List internally) it redirects (with 302 Object Moved) back to /Toons/Add.
The problem goes away if I use .aspx hack (that works in IIS6/IIS7 classic mode).
But without .aspx - GET work fine, but POST redirects me onto itself but with GET.
What am I missing?
I'm hosting with webhost4life.com and they did change IIS7 to integrated mode already.
EDIT: The code works as expected using UltiDev Cassini server.
EDIT: It turned out to be trailing-slash-in-URL issue. Somehow IIS7 doesn't route request properly if there is no slash at the end.
EDET: Explanation of the behavior
What happens is when I request (POST)/Toons/List
(without trailing slash), IIS doesn't find the handler (I do not have knowledge to understand how exactly IIS does URL-to-handler mapping) and redirects the request (using 302 code) to /开发者_开发技巧Toons/List/
(notice trailing slash).
A browser, according to the HTTP specification, must redirect the request using same method (POST in this case), but instead it handles 302 as if it is 303 and issues GET request for the new URL.
This is incorrect, but known behavior of most browsers.
The solution is either to use .aspx-hack to make it unambiguous for IIS how to map requests to ASP.NET handler, or configure IIS to handle everything in the virtual directory using ASP.NET handler.
Q: what is a better way to handle this?
You have control over your code. change all pages that make a post without the trailing slash to post to the correct page. if its 3rd party clients, than return an exception, that they should fix the bug.
this is expected behaviour, and its not your job to recover everything that can happen. but you should give good hints (e.g. exception message, instead of a weird error or redirect.)
精彩评论