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


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 trying to overload methods.

How to get all input controls which are of type file in jquery
You'd be better off leaving it as one method with an if/else, or making another Action that responds specifically to the Ajax request..
Razor view engine - exception when calling Any Function

Write css for individual page in mvc3 project

Asp.net WebForms app that runs MVC in a subfolder


how do i have a facebook-like scrolling updates on my website showing all activity
The MVC framework 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 do this, you can write your own ActionFilter..


I agree that it would be cleaner to be able to choose an action based on the return type.

You could implement your own IActionInvoker class to do this..

75 out of 100 based on 20 user ratings 245 reviews