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

1

21

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?

Nunes answered 10/7, 2011 at 18:7 Comment(0)
S
47

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") }
);
Sisyphean answered 11/7, 2011 at 5:5 Comment(3)
Thanks a lot ;) That is what I was missing ;)Nunes
If each line is respectively prefixed with "name:", "url:", "defaults:" => what do you prefix the "new { httpMethod..." line with ?Paneling
@Paneling Prefix is just the name of the parameter in method declaration, which is constraints in this case.Ghana

© 2022 - 2024 — McMap. All rights reserved.