简体   繁体   English

重载ASP.NET MVC操作

[英]Overload ASP.NET MVC Actions

How can I overload actions in ASP.NET MVC, but with support for GET QueryString? 如何重载ASP.NET MVC中的操作,但是支持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 . 但每当我访问/controller/find?q=abc/controller/find?q=abc&isBlaBla=false它会抛出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 如果您使用的是C#4.0,甚至可以使用Optional Parameters和Name Arguments

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

you should be using routes eg find/abc or find/abc/false 你应该使用路线,例如find/abcfind/abc/false

if you must use a query string u can use no arguments and access the querystring in the HttpContext 如果必须使用查询字符串,则不能使用任何参数并访问HttpContext的查询字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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