Alon Gubkin
Alon Gubkin

Reputation: 57119

Overload ASP.NET MVC Actions

How can I overload actions in ASP.NET MVC, but with support for GET QueryString? I tried to do something like this:

public JsonResult Find(string q)
{
    ...
}

public JsonResult Find(string q, bool isBlaBla)
{
    ...
}

But whenever I access /controller/find?q=abc or /controller/find?q=abc&isBlaBla=false it throws anSystem.Reflection.AmbiguousMatchException.

How to fix this?

Upvotes: 5

Views: 2392

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

ASP.NET doesn't support action overloading with the same HTTP verb.

Upvotes: 1

willbt
willbt

Reputation: 1885

You actually don't need to create overloads. All you need to do is create a single action method with all the possible arguments that you expect and it will map the values (where possible) for you.

public JsonResult Find(string q, bool isBlaBla)
{

}

You could even make use of Optional Parameters and Name Arguments if you're using C# 4.0

Upvotes: 2

Paul Creasey
Paul Creasey

Reputation: 28824

you should be using routes e.g. find/abc or find/abc/false

if you must use a query string u can use no arguments and access the querystring in the HttpContext

Upvotes: 0

Related Questions