Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC : strange POST behavior

ASP.NET MVC 2 app

I have two actions on my controller (Toons):

  1. [GET] List
  2. [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?

like image 845
THX-1138 Avatar asked Nov 14 '22 13:11

THX-1138


1 Answers

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.)

like image 129
cRichter Avatar answered Dec 11 '22 02:12

cRichter