简体   繁体   English

Asp.Net Web Api:处理两个具有相同签名的获取请求

[英]Asp.Net Web Api : Handling two get request with same signature

Current situation现在的情况

    [HttpGet]
    public HttpResponseMessage CheckProfileStatus(int id)
    {
        //check profile status from third party
        return //profile status and HttpStatus;
    }

    [HttpGet]
    public HttpResponseMessage ProcessTheProfile(int profileId)
    {
        //check profile status from third party again, if status is "Good", do something
        return //someMessage and HttpStatus;
    }

Url to call CheckProfileStatus method调用CheckProfileStatus方法的 URL

myUrl/api/Profile/123

Url to call ProcessTheProfile method调用ProcessTheProfile方法的 URL

myUrl/api/Profile/?profileId=123

I hope the comments in the code makes the situation clear.我希望代码中的注释使情况清楚。

I don't really like the fact that I have two HttpGet methods with same signature in one controller (even though it works).我真的不喜欢我在一个控制器中有两个具有相同签名的HttpGet方法(即使它有效)这一事实。

I don't know if it is best practice.我不知道这是否是最佳实践。

Question:题:

Should I extract these two methods in two seperate controllers, is it completely fine to have two HttpGet methods with same signature in one controller or is there better way to handle this situation?我应该在两个单独的控制器中提取这两种方法,在一个控制器中有两个具有相同签名的 HttpGet 方法是完全没问题的,还是有更好的方法来处理这种情况?

First, this is confusing to any client of the API. 首先,这会使API的任何客户端感到困惑。

You have 2 GET methods which are virtually identical. 您有2个GET方法几乎相同。 In fact most people prefer to add a route covering your first option which basically sets the profileId to the value after the forward slash. 实际上,大多数人都喜欢添加一条覆盖您的第一个选项的路由,该路由基本上将profileId设置为正斜杠后的值。

Second, the expectation is that when you issue a GET against an entity with an ID, you get the representation of that entity. 其次,期望是当您对具有ID的实体发出GET时,会得到该实体的表示形式。 This is not what's happening here. 这不是这里发生的事情。

You need to decide which kind of API do you want. 您需要确定所需的API。 A generic one where stuff like this is fine : 一个通用的,这样的东西很好:

myUrl/api/profile/process/123 or process/profile , or whatever else makes sense to the API. myUrl/api/profile/process/123process/profile ,或其他对API有意义的内容。

If your API is supposed to be RESTful ( which basically means you will have one method per HTTP verb and nothing more ) then you need to add a new controller for ProcessProfile and then your url can look like this : 如果您的API应该是RESTful的 (基本上意味着您每个HTTP动词只有一个方法,仅此而已),那么您需要为ProcessProfile添加一个新的控制器,然后您的url如下所示:

myUrl/api/ProcessProfile/123

None of these options is set in stone, they are just that, options. 这些选项都不是一成不变的,只是这些选项。 The comments to the OP thread give some good options as well, which should be considered. 对OP线程的注释也提供了一些不错的选择,应予以考虑。

In my opinion, this approach will cause problems if your project grows bigger since it's confusing to use. 我认为,如果您的项目变得越来越大,因为使用起来很混乱,那么这种方法就会引起问题。

I would suggest that you use a RESTful approach. 我建议您使用RESTful方法。

Maybe this article will help you a bit in understanding. 也许本文会帮助您有所了解。

https://medium.com/@schneidenbach/restful-api-best-practices-and-common-pitfalls-7a83ba3763b5 https://medium.com/@schneidenbach/restful-api-best-practices-and-common-pitfalls-7a83ba3763b5

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

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