简体   繁体   中英

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 an System.Reflection.AmbiguousMatchException .

How to fix this?

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

ASP.NET不支持使用相同的HTTP谓词进行操作重载。

you should be using routes eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM