views:

77

answers:

3

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?

A: 

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

Paul Creasey
+1  A: 

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

willbt
Yes, but the idea in two overloads is if in the url you don't specify isBlaBla, it redirects to the first overload. Should I use `Nullable<bool> isBlaBla`?
TTT
Well two overloads of the same name that accept the GET verb is not possible. Making isBlaBla? nullable will work.
willbt
I've tried this before and came to the conclusion that ASP.NET MVC doesn't support method overloading. I could be wrong though.
Daniel T.
As I stated above it's not possible and with good reason too. Having said that it is possible to have the same Action name as long as the AcceptVerbs is different (so one is GET and the other POST).
willbt
A: 

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

Darin Dimitrov