2

Can somebody explain me one thing.

I have two methods in my controller :

public ActionResult AddPredefinedTicket(int customerId) {...}

and

public ActionResult AddPredefinedTicket(int customerId, TicketTypes type, string additionalJsonParameters) {...} (here TicketTypes is enum)

I'm tring to make a call with using URL like

http://.../Ticket/AddPredefinedTicket?customerId=1082

For some reason i got an exception :

The current request for action 'AddPredefinedTicket' on controller type 'TicketController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult AddPredefinedTicket(Int32) on type CallCenter.CustomerService.Controllers.TicketController System.Web.Mvc.ActionResult AddPredefinedTicket(Int32, CallCenter.CustomerService.Data.Models.TicketTypes, System.String) on type CallCenter.CustomerService.Controllers.TicketController

But, i don't understand why MVC think that the request is ambiguous. As you can see from my URL call, i'm not passing neither 'type' or 'additionalJsonParameters' parameters. I understand, that additionalJsonParameters is string, so it can be null.

But the action also has "type" parameter, that is enum and can't be null.

In my oppinion, MVC should use first action, but it don't.

Can you explain why ?

Andrey Tagaew
  • 1,573
  • 1
  • 15
  • 20
  • This is a duplicate http://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc – Mathias F Mar 31 '10 at 12:52

3 Answers3

1

Did you forgot to decorate your methods with [HttpGet], [HttpPost] attributes.

AJ.
  • 621
  • 4
  • 8
0

you cannot overload your ActionResults.

"There are some additional requirements that must be satisfied by a controller action. A method used as a controller action cannot be overloaded. Furthermore, a controller action cannot be a static method. Other than that, you can use just about any method as a controller action."

See : http://www.asp.net/Learn/mvc/tutorial-03-cs.aspx

OhBugger
  • 74
  • 6
  • Thanks Man, now i see that its impossible. But, i still wonder, why this case was not implemented in MVC ? Don't you know why MVC has such limitation ? – Andrey Tagaew Mar 31 '10 at 13:24
0

Place the logic from the first method in the second method.

Put a check in there like

if(type==null && string.IsNullOrEmpty(additionalJsonParameters){
//do logic from method 1
}
else{
//do logic from method 2
}
kmehta
  • 2,457
  • 6
  • 31
  • 37
  • Yes, this is an option. But i really hate such if/then logic in controller. For me it will be really better to use different names of actions – Andrey Tagaew Apr 01 '10 at 23:24