Disambiguate controller actions of same name based on result type?


Disambiguate controller actions of same name based on result type?



Is there a way to force the request router to disambiguate controller actions based on method signatures?

for instance(in a controller):

[HttpPost] public ActionResult MyAction(FormCollection f) {     //do stuff with big form and return viewresult     return View(); }  [HttpPost] public JsonResult MyAction(string foo, string bar, bool baz) {     //separate method to handle just the ajax stuff     return Json(); } 

I know I can use Request.IsAjaxRequest() in the first method and use that if/then branch to decided my action... but it feels cleaner to leave them separate methods. Not to mention I'd have to cast out the boolean 'baz' (and any other of the non-string types). I have a legitimate reason to use the FormCollection at times or else I'd just keep the JsonResult version around. (the formcollection allows editing of 'many' items while the Json parametrized version just edits a single item)

I could just use different method names too... (probably what I'll do if I can't do what I want)

I tried setting the return type of the 1st method explicitly to ViewResult and ensuring the client sent only accept: application/json headers, but doesn't seem to matter as I keep getting exceptions about the method signatures being ambiguous. Is there a way to force the behavior I want?


Email errors in a asp.net mvc application

1:

ASP.NET MVC: First access after some minutes slow, then every following request is fast
From what I've experienced, ASP.NET MVC does not really enjoy endeavor to overload methods. How to get all input controls which are of type file in jqueryYou'd be better off leaving it as one method with an if/else, or making ananother Action this responds specifically to the Ajax request.. Razor view engine - exception when calling Any Function
Write css for individual page in mvc3 projectAsp.net WebForms app that runs MVC in a subfolder

2:

how do i have a facebook-like scrolling updates on my website showing all activity
The MVC framejob does not use the method's return type to filter methods.. Razor view engine - How can I add Partial Views If you really want to did this, you must write your own ActionFilter..

3:

I agree this it would be cleaner to be able to choose an action based on the return type. You could implement your own IActionInvoker class to did this..


65 out of 100 based on 40 user ratings 675 reviews