我可以在Web API中使用多种具有不同复杂参数类型的POST方法吗? [英] Can I have multiple POST methods in Web API with different complex parameter types?

查看:56
本文介绍了我可以在Web API中使用多种具有不同复杂参数类型的POST方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Web API的新手...这是我的基本路线:

I'm new to Web API... Here's my basic route:

config.Routes.MapHttpRoute(
  name: "Workitems",
  routeTemplate: "api/{controller}/{workitemID}",
  defaults: new { controller = "workitems", workitemID = RouteParameter.Optional }
);

这就是我想要的:

public HttpResponseMessage Post( [FromBody] FolderModel theModel )
public HttpResponseMessage Post( [FromBody] DocumentModel theModel )

但是,Web API找不到我的第二个Post方法.我已经在这里和Google中进行了大量搜索,但没有找到任何适合我的内容(好).我知道我可以在第二种方法中添加第二种未使用的参数-但这太过分了.如果这是普通的C#代码,则编译器将毫无疑问地知道选择哪种方法具有不同的签名.但是Web API不够智能.

But, Web API doesn't find my second Post method. I've done lots of searching here and in Google but haven't found anything that works for me (well). I know I could add a 2nd unused parameter to the 2nd method - but that's too much of a hack. If this were normal C# code, the compiler would have no problem knowing which to choose b/c the methods have different signatures. But Web API is not smart enough.

我查看了自定义约束,但这似乎不合适.我也不能使用其他{actions},因为这违反了我的API的RESTful约束(没有RPC,只有资源).我也不能将第二个帖子放在其他控制器上.

I looked at custom constraints but that didn't seem appropriate. I also cannot use different {actions} as that violates RESTful constraints (no RPC, just resources) for my API. I also cannot put the 2nd Post on a different controller.

使它起作用的唯一方法是将FolderModel和DocumentModel都包装在这样的父对象中:

The only way I've gotten this to work is to wrap both FolderModel and DocumentModel in a parent object like this:

public class WorkitemCreateModel
{
    public DocumentModel Document { get; set; }
    public FolderModel Folder { get; set; }
}

public HttpResponseMessage Post( [FromBody] WorkitemCreateModel theModel )

然后有一个采用WorkitemCreateModel的Post方法.但是使用我的API的开发人员有责任,他们必须传入WorkitemCreateModel,但必须仅传入DocumentModel对象或FolderModel对象.太烦人了,因为我的GET API可以返回DocumentModel对象或FolderModel对象.因此,将您从GET获得的对象传递到POST中将是一个很好的选择.但这是行不通的,他们必须先将其包装在WorkitemCreateModel对象中.

Then have a single Post method that takes WorkitemCreateModel. But then it's the responsibility of the developer using my API that they must pass in WorkitemCreateModel but they must only pass in a DocumentModel object OR a FolderModel object. It's annoying too b/c my GET API can return either a DocumentModel object or a FolderModel object. So, it would be nice to just pass the object you get from the GET into the POST. But that doesn't work and they must wrap it in a WorkitemCreateModel object first.

还有其他建议吗?

顺便说一句:这个网站是最好的!我在这里找到了很多答案!

BTW: this website is the best! I've found SO many answers here!

推荐答案

这可能是一篇过时的文章,但我添加此信息是为了防备像我这样来这里寻求答案的人.

This might be an old post but I am adding this info just in case for people like me who came here looking for answers.

这里的简短答案是 否,这不可能 .

The short answer here is NO, its not possible.

问题在于路由的工作方式,尤其是有关选择使用哪种操作方法的部分.这是ASP .NET文章的摘录( https://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection ),

The problem is with the way the routing works, especially the part about choosing what action method to use. Here is an extract from ASP .NET article (https://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection),

选择算法的目标是在调用任何绑定之前从静态描述中选择一个动作.因此,复杂类型会从匹配算法中排除.

The goal of the selection algorithm is to select an action from the static description, before invoking any bindings. Therefore, complex types are excluded from the matching algorithm.

因此,在将操作方法​​与路径匹配时,Web API 忽略该方法的参数列表中的所有复杂类型,并且当您这样做时,两个方法都具有0个参数,这就是您要面对的原因这个问题.

So while matching action methods to the path, Web API disregards all Complex types in the parameter list for that method and when you do that both of your methods have 0 parameters and that's why you are facing this problem.

希望这对您有帮助...

Hope this helps...

这篇关于我可以在Web API中使用多种具有不同复杂参数类型的POST方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆