Ultimate ASP.NET Core Web API 22 WORKING WITH OPTIONS AND HEAD REQUESTS

22 WORKING WITH OPTIONS AND HEAD REQUESTS
22 使用 OPTIONS 和 HEAD 请求

In one of the previous chapters (Method Safety and Method Idempotency), we talked about different HTTP requests. Until now, we have been working with all request types except OPTIONS and HEAD. So, let’s cover them as well.‌
在前面的一章(方法安全和方法幂等性)中,我们讨论了不同的 HTTP 请求。到目前为止,我们一直在处理除 OPTIONS 和 HEAD 之外的所有请求类型。那么,让我们也介绍一下它们。

22.1 OPTIONS HTTP Request

22.1 OPTIONS HTTP 请求

The Options request can be used to request information on the communication options available upon a certain URI. It allows consumers to determine the options or different requirements associated with a resource. Additionally, it allows us to check the capabilities of a server without forcing action to retrieve a resource.‌
Options 请求可用于请求有关特定 URI 上可用的通信选项的信息。它允许使用者确定与资源关联的选项或不同要求。此外,它还允许我们检查服务器的功能,而无需强制作来检索资源。

Basically, Options should inform us whether we can Get a resource or execute any other action (POST, PUT, or DELETE). All of the options should be returned in the Allow header of the response as a comma- separated list of methods.
基本上,Options 应该告诉我们是否可以获取资源或执行任何其他作(POST、PUT 或 DELETE)。所有选项都应在响应的 Allow 标头中作为逗号分隔的方法列表返回。

Let’s see how we can implement the Options request in our example.
让我们看看如何在示例中实现 Options 请求。

22.2 OPTIONS Implementation

22.2 OPTIONS 实现

We are going to implement this request in the CompaniesController — so, let’s open it and add a new action:‌
我们将在 CompaniesController 中实现此请求 — 因此,让我们打开它并添加新作:

[HttpOptions] public IActionResult GetCompaniesOptions() { Response.Headers.Add("Allow", "GET, OPTIONS, POST"); return Ok(); }

We have to decorate our action with the HttpOptions attribute. As we said, the available options should be returned in the Allow response header, and that is exactly what we are doing here. The URI for this action is /api/companies, so we state which actions can be executed for that certain URI. Finally, the Options request should return the 200 OK status code. We have to understand that the response, if it is empty, must include the content-length field with the value of zero. We don’t have to add it by ourselves because ASP.NET Core takes care of that for us.
我们必须使用 HttpOptions 属性来装饰我们的 action。正如我们所说,可用选项应该在 Allow 响应标头中返回,这正是我们在这里所做的。此作的 URI 是 /api/companies,因此我们说明可以针对该特定 URI 执行哪些作。最后,Options 请求应返回 200 OK 状态代码。我们必须了解,如果响应为空,则必须包含值为零的 content-length 字段。我们不必自己添加它,因为 ASP.NET Core 会为我们处理这些。

Let’s try this:
让我们试试这个:
https://localhost:5001/api/companies

alt text

As you can see, we are getting a 200 OK response. Let’s inspect the Headers tab:
如您所见,我们收到了 200 OK 响应。我们来检查 Headers 选项卡:

alt text

Everything works as expected.
一切都按预期进行。

Let’s move on.
让我们继续前进。

22.3 Head HTTP Request

22.3 头 HTTP 请求

The Head is identical to Get but without a response body. This type of request could be used to obtain information about validity, accessibility, and recent modifications of the resource.‌
Head 与 Get 相同,但没有响应正文。这种类型的请求可用于获取有关资源的有效性、可访问性和最近修改的信息。

22.4 HEAD Implementation

22.4 HEAD 实现

Let’s open the EmployeesController, because that’s where we are going to implement this type of request. As we said, the Head request must return the same response as the Get request — just without the response body. That means it should include the paging information in the response as well.‌
让我们打开 EmployeesController,因为这是我们要实现此类请求的地方。正如我们所说,Head 请求必须返回与 Get 请求相同的响应 — 只是没有响应正文。这意味着它还应该在响应中包含分页信息。

Now, you may think that we have to write a completely new action and also repeat all the code inside, but that is not the case. All we have to do is add the HttpHead attribute below HttpGet:
现在,您可能认为我们必须编写一个全新的作并重复其中的所有代码,但事实并非如此。我们所要做的就是在 HttpGet 下面添加 HttpHead 属性:

[HttpGet] [HttpHead] public async Task<IActionResult> GetEmployeesForCompany(Guid companyId, [FromQuery] EmployeeParameters employeeParameters)

We can test this now:
我们现在可以测试一下:

https://localhost:5001/api/companies/C9D4C053-49B6-410C-BC78-2D54A9991870/employees?pageNumber=2&pageSize=2

alt text

As you can see, we receive a 200 OK status code with the empty body.Let’s check the Headers part:
如您所见,我们收到一个 200 OK 状态代码,其中正文为空。让我们检查一下 Headers 部分:

alt text

You can see the X-Pagination link included in the Headers part of the response. Additionally, all the parts of the X-Pagination link are populated — which means that our code was successfully executed, but the response body hasn’t been included.
您可以看到响应的 Headers 部分中包含的 X-Pagination 链接。此外,X-Pagination 链接的所有部分都已填充 — 这意味着我们的代码已成功执行,但响应正文尚未包含。

Excellent.
非常好。

We now have support for the Http OPTIONS and HEAD requests.
我们现在支持 Http OPTIONS 和 HEAD 请求。

Leave a Reply

Your email address will not be published. Required fields are marked *