The HTTP standard is a dynamic document that is regularly reviewed and updated. The specification may be “done” to some developers’ eyes, but there are a ton of new and intriguing HTTP methods that are about to be proposed. The QUERY method, which has semantics identical to a GET method call but allows users to submit a request body, is one such HTTP method. As you might guess, this opens up a world of possibilities for searches that are more complicated than what would be possible with a standard query string.

In this article, we’ll go through how to enable ASP.NET Core to support experimental HTTP methods. We’ll also go through some reasons why you might not want to. Let’s get going.

The HTTP Query Method: What is it?

We should discuss HTTP Query and why others would want to utilize it in their API implementations before moving on to the C# implementation.

Queries are often delivered via the GET or POST method when developers create HTTP APIs. The URL path and the query parameters are included in GET requests. While complicated queries can be transmitted using the GET method, this approach is constrained by the HTTP standard. A URL may only contain a certain amount of data, and items in the query string must be URL encoded. The POST method, which permits more complicated query syntax in the request body, is frequently used by developers when they approach the boundaries of utilizing the GET method. Nevertheless, utilizing POST has disadvantages as well because these calls do not adhere to the same caching restrictions as GET requests and as a result, place greater strain on the host server.

With regard to caching and idempotency semantics, the QUERY method combines the advantages of the GET method with the flexibility for developers to publish a body akin to a publish request. Developers may now send any message to the server, even those using sophisticated query languages like GraphQL, SQL, LINQ, and others, thanks to the inclusion of the body.

A New HTTP Method Should Be Added To Simple API Endpoints

Fortunately, HTTP method detection in ASP.NET Core may be customized. As a result, you don’t need a lot of plumbing code to add any HTTP method. Let’s start by giving a Minimal API endpoint the QUERY function.

You may utilize the MapMethods method on IEndpointConventionBuilder when working with Minimal APIs. Any string value that ASP.NET Core could perceive as an HTTP method can be sent into the method. You may also create a MapQuery extension method to match the existing methods of MapGet, MapPost, etc. for user convenience.

A <FuncQuery,IResult> is present in our extension function, as you would have seen. The purpose of the Query type is to model-bind our request’s body to a string. Examining this class, you may choose to implement the reading of the body in any manner you choose, perhaps including serializing it into a strongly-typed entity. However, doing so is absolutely optional.

Let’s finally put our objective into practice. I’ll turn the request’s body into a LINQ expression in this illustration. This shouldn’t be done in production since an expression is C# code and might create security flaws. This sample is being used as a demo.

Later on, we’ll call this endpoint, but for now, let’s also give ASP.NET Core MVC QUERY support.

Enhance ASP.NET Core MVC With A New HTTP Method

It is considerably simpler to integrate experimental HTTP methods into ASP.NET Core MVC. Only a class inherited from HttpMethodAttribute must be implemented.

Once it has been implemented, all that is left to do is add the new attribute to your ASP.NET Core MVC method. Let’s have a look at an action that can now process QUERY queries as the final implementation.

Because ASP.NET Core MVC’s model binding can be too complex in this instance, I’ve avoided using it. Please be considerate and email me a link to your implementation if you decide to try to build it yourself. Before fully implementing the QUERY method, you might wish to take into account your query approach.

Great! Now that a Query call from a client can be responded to, we have a Minimal API endpoint and an ASP.NET Core MVC action. Let’s look at the call method for these endpoints.

Calling our QUERY Endpoints and Actions

The following is the first restriction you’ll probably encounter while using experimental HTTP methods: The majority of the tooling you use is unaware of the method’s existence. Unfortunately, you can’t use tools like Postman, Swagger, and similar tools because of the experimental nature. Your ally is the code. Fortunately, using the HttpClient class to call the APIs is rather simple.

Despite the fact that the request might be made by any C# code housed in any code base, I’ll utilize another GET endpoint to access our Query API.

The use of the HttpMethod class is the main lesson to be learned from the code above. Any string value can be entered, and in our case, we want to set the method to QUERY. Additionally, a LINQ expression will be sent to filter our dataset. The remaining code is normal for a HttpClient application.

When our GET endpoint is called, our Query endpoints are also called, and our results are then obtained.

Effects of Experimentation

While considering and trying out experimental HTTP methods in your APIs can be interesting, you should first weigh the downsides.

  1. Because it’s experimental, the specification could change after you put it into practice.
  2. You’ll probably have to code all interactions because your preferred tools won’t likely support your experimental techniques.
  3. Typical ASP.NET Core libraries won’t function as intended. For instance, Swashbuckle (The OpenAPI library) will not materialize your endpoint schema in ASP.NET Core.
  4. I am unaware of any infrastructure-related advantages of caching, load balancers, etc. Even worse, you could cause problems with your infrastructure.


As long as you know what to implement, ASP.NET Core allows you to add any method. This was an enjoyable experiment that, if nothing else, clarified certain endpoint resolution methods.

Thank you for reading. Happy coding!

Leave a comment

Your email address will not be published.