简体   繁体   English

如何在API控制器中有两个名称相同但参数不同的方法?

[英]How to have two methods in API Controller with same name but different arguments?

I'm new to ApiControllers in MVC4 and I need to have to Get in my Api Controller with different set of arguments like below: 我是MVC4中的ApiController的新手,我需要使用以下类似的不同参数集进入Api Controller:

public Models.Response Get(int skip, int take, int pageSize, int page)
{
    //do something
}

public Models.Response Get(int skip, int take, int pageSize, int page, PersonSearchModel personSearchModel)
{
    //search with search model
}

I make a string of "PersonSearchModel" properties and my request look like this: (the instance of search model was empty) 我创建了一个字符串“ PersonSearchModel”,并且我的请求看起来像这样:(搜索模型的实例为空)

localhost:3039/api/personapi/?Firstname=&Lastname=&BirthDate=1/1/0001%2012:00:00%20AM&Gender=0&PageIndex=0&PageSize=20&SortExpression=&TotalItemCount=0&TotalPageCount=0&&take=3&skip=0&page=1&pageSize=3 本地主机:3039 / api / personapi /?Firstname =&Lastname =&BirthDate = 1/1/0001%2012:00:00%20AM&Gender = 0&PageIndex = 0&PageSize = 20&SortExpression =&TotalItemCount = 0&TotalPageCount = 0 && take = 3&skip = 0&page = 1&pageSize = 3

based on what I know from MVC3 it's supposed to map the url to search model and choose the second Get but I get the "Multiple actions were found that match the request" Exception in my firebug. 根据我从MVC3中了解到的信息,应该将URL映射到搜索模型并选择第二个Get,但是在萤火虫中出现了“发现多个与请求匹配的动作”异常。 what should I do? 我该怎么办? thanks 谢谢

One thing you cannot do in MVC in the controller is overload a functions. 您不能在控制器的MVC中做的一件事就是重载函数。

For the extra parameter, set it as optional and check for the default value you assign it. 对于额外的参数,请将其设置为可选,并检查您为其分配的默认值。

You could write a custom attribute derived from ActionMethodSelectorAttribute that would check the request parameters. 您可以编写一个从ActionMethodSelectorAttribute派生的自定义属性,该属性将检查请求参数。 You need to oveeride the IsValidForRequest method. 您需要选择IsValidForRequest方法。 It could be something like 可能是这样的

public class RequireRequestValueAttribute : ActionMethodSelectorAttribute
{
    public RequireRequestValueAttribute(valueName)
    {
        ValueName = valueName;
    }
    public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
    {
        return (controllerContext.HttpContext.Request[ValueName] != null);
        }
    }
    public string ValueName { get; private set; }
} 

(You could extend it to check for more than one parameter) (您可以扩展它以检查多个参数)

You use this attribute with your methods like this 您可以将这种属性与类似的方法一起使用

public Models.Response Get(int skip, int take, int pageSize, int page)
{
    //do something
}

[RequireRequestValue("personSearchModel")]
public Models.Response Get(int skip, int take, int pageSize, int page, PersonSearchModel personSearchModel)
{
    //search with search model
}

This works for me with MVC 3 and I suppose it also should for MVC 4 这适用于我与MVC 3,我想它也应适用于MVC 4

暂无
暂无

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

相关问题 如何使一个路由指向两个不同的控制器端点,该端点在WEB Api 2中接受不同的参数 - How to have a Route which points to two different controller end points which accepts different arguments in WEB Api 2 如何使用名称相同但签名不同的两种方法创建通用接口? - How to create an Generic Interface with two methods with the same name but different signature? 两种不同方法中的参数名称相同 - Same name in parameter in two different methods 控制器方法具有相同名称 - Controller Methods with same name 如何使用两个相同的类名和具有不同返回类型的相同辅助方法重构此代码? - How to refactor this code with two same class name and same helper methods with different return types? 我们不能为不同的类提供相同名称的扩展方法吗? - Can't we have extension methods with same name for different classes? 将两种方法重构为一种具有相同字段但名称不同的方法 - Refactoring two methods into one that have the same fields but different names 在MVC3中,是否可以在不同的区域中使用相同的控制器名称? - Is it possible, in MVC3, to have the same controller name in different areas? 具有相同名称但HTTP方法不同的Web API自定义路由 - Web API custom routes with same name but different HTTP methods 是否可以在 Azure Api Management 的同一项目中为两个不同的服务(控制器中的方法)使用两个不同的 API 密钥? - Is it possible to use two different API Keys for two different services (methods in controllers) in same project in Azure Api Management?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM