Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an endpoint filter to translate the Result object to IResult #49

Closed
MrDave1999 opened this issue Dec 10, 2023 · 0 comments · Fixed by #50
Closed

Add an endpoint filter to translate the Result object to IResult #49

MrDave1999 opened this issue Dec 10, 2023 · 0 comments · Fixed by #50
Labels
feature New feature or request

Comments

@MrDave1999
Copy link
Owner

The filter only converts the Result object to an implementation of Microsoft.AspNetCore.Http.IResult.

Example

public static class PersonEndpoint
{
    public static void AddRoutes(this WebApplication app)
    {
        var personGroup = app
            .MapGroup("/Person")
            .WithTags("Person")
            .AddEndpointFilter<TranslateResultToHttpResultFilter>();

        personGroup
            .MapPost("/", ([FromBody]Person person, PersonService service) => service.Create(person))
            .Produces<Result>();

        personGroup
            .MapGet("/", (PersonService service) => service.GetAll())
            .Produces<ListedResult<Person>>();
    }
}

TranslateResultToHttpResultFilter must implement the IEndpointFilter interface.

The detail to take into consideration is that IEndpointFilter is only available as of ASP.NET Core 7.0. This means that the custom filter should be compiled for specific frameworks such as .NET 7, 8 or higher.

As the SimpleResults.AspNetCore project has multiple target frameworks, the preprocessor symbols could be used as NET7_0_OR_GREATER. See https://learn.microsoft.com/en-us/dotnet/core/tutorials/libraries#preprocessor-symbols

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant