开发者

Overload ASP.NET MVC Actions

开发者 https://www.devze.com 2023-01-14 15:35 出处:网络
How can I overload actions in ASP.NET MVC, but with support for GET QueryString? I tried to do something like this:

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 /con开发者_运维百科troller/find?q=abc&isBlaBla=false it throws anSystem.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 doesn't support action overloading with the same HTTP verb.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号