In a previous post, “Using Carter to Create A ASP.NET WebAPI With QUERY Support in C# & .NET”, we looked at how to add HTTP Query support to a Carter module.

At present, unlike the other verbs GET, POST, PUT, DELETE, PATCH, there are no ASP.NET equivalents (MapGet, MapPost, MapPut, MapDelete, MapPatch) for QUERY. At least not yet.

In our previous post, “Implementing MapQuery For ASP.NET in C# & .NET”, we implemented an extension method for this.

It looked like this:

public static class QueryEndpointExtensions
{
    public static RouteHandlerBuilder
        MapQuery(this IEndpointRouteBuilder endpoints, string pattern, Delegate handler) =>
        endpoints.MapMethods(pattern, [HttpMethods.Query], handler);
}

We shall reuse this for our API.

This means that we change this:


public class SearchModule : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        app.MapMethods("/", [HttpMethods.Query], (SearchRequest request) =>
        {
            var response =
                $"You requested {request.SearchText} from database {request.Database} sorted by {request.SortColumn}";
            return response;
        });
    }
}

To this:

public class SearchModule : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        app.MapQuery("/", (SearchRequest request) =>
        {
            var response =
                $"You requested {request.SearchText} from database {request.Database} sorted by {request.SortColumn}";
            return response;
        });
    }
}

Our API should work as expected:

carterQuery

And return the expected results:

carterQueryResults

TLDR

You can add support to Carter for HTTP Query via an extension method.

The code is in my GitHub.

Happy hacking!