Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 894
TomTom Avatar asked Jul 10 '11 18:07

TomTom


People also ask

How pass data from one controller to another in MVC?

ViewData, ViewBag, and TempData are used to pass data between controller, action, and views. To pass data from the controller to view, either ViewData or ViewBag can be used. To pass data from one controller to another controller, TempData can be used.

How can use route in MVC controller?

Configure a Route Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder. The following figure illustrates how to configure a route in the RouteConfig class .

Can we have multiple controllers in MVC?

In Spring MVC, we can create multiple controllers at a time. It is required to map each controller class with @Controller annotation.

How does MVC routing work?

ASP.NET MVC Routing does the same thing; it shows the way to a request. Basically, routing is used for handling HTTP requests and searching matching action methods, and then executing the same. It constructs outgoing URLs that correspond to controller actions. Routing the map request with Controller's Action Method.


1 Answers

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") } ); 
like image 186
mrydengren Avatar answered Oct 05 '22 06:10

mrydengren