MrDave1999 / SimpleResults

A simple library to implement the Result pattern for returning from services
https://mrdave1999.github.io/SimpleResults
MIT License
105 stars 2 forks source link

Allow endpoint filter to be used in controller actions #51

Closed MrDave1999 closed 11 months ago

MrDave1999 commented 11 months ago

PR #50 only works if you use in Minimal API but in controller actions it does not work.

See https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/min-api-filters?view=aspnetcore-8.0#register-a-filter-on-controller-actions

MrDave1999 commented 11 months ago

The problem is this line: https://github.com/MrDave1999/SimpleResults/blob/95761227860a7309c9b65551476270d1f3440aea/src/AspNetCore/TranslateResultToHttpResultFilter.cs#L26-L28

By default, all values returned by controller actions are wrapped in a type called Microsoft.AspNetCore.Mvc.ObjectResult. This means that this condition will be false and the translator will never be called.

MrDave1999 commented 11 months ago

There is no benefit in resolving this issue. The endpoint filter called TranslateResultToHttpResult was created specifically for the Minimal APIs. For controller actions, you must use the attribute called TranslateResultToActionResult.