MVC: Route Get / Post to different controllers. How?

TomTom picture TomTom · Jul 10, 2011 · Viewed 8.9k times · Source

I am writing a MVC controller where I need to handle both, data return as well as a long poll "data has changed" like behavior from the SAME (!) url. NothingI can do about this - I am implementing a proxy for an already existing application, so I have no way to do any extensions / modifications to the API.

My main problem is: * The POST operations have to be completed immediately. * The GET operations take longer (can take hours sometimes).

Can I somehow rewrite both to go to different controllers? The alternative would be to... hm... make both async, just the POST is finishing right three and then.

Anyone a comment on that?

Answer

mrydengren picture mrydengren · Jul 11, 2011

You should be able to use constraints at the routing level to control which controller/action the url goes to.

routes.MapRoute(
    "route that matches only GETs for your url",
    "your url",
    new { controller = "some controller", action = "some action" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);

routes.MapRoute(
   "route that matches only POSTs for your url",
   "your url",
    new { controller = "some other controller", action = "some other action" },
    new { httpMethod = new HttpMethodConstraint("POST") }
);