I have the following custom conventions configuration in my ASP.NET WebAPI project Program.cs file
builder.Services
.AddControllers(mvcOptions => mvcOptions
.AddResultConvention(resultStatusMap => resultStatusMap
.AddDefaultMap()
.For(ResultStatus.Invalid,
HttpStatusCode.BadRequest,
resultStatusOptions => resultStatusOptions
.With<ExtendedProblemDetailsResponse>((_, result) => ExtendedProblemDetailsResponse.FromResult(result))))).AddJsonOptions(opts =>
{
opts.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
opts.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
opts.JsonSerializerOptions.Converters.Add(new DateTimeOffsetConverter());
});
I have the following endpoint
[HttpPut("{salesAgentId}")]
public async Task<IActionResult> CreateProspect([FromRoute] Guid salesAgentId,
[FromBody] CreateProspectRequest request,
CancellationToken cancellationToken = default)
{
var result = await handler.CreateAsync(request.ToCommand(salesAgentId,
timeProvider.GetUtcNow()),
cancellationToken);
return result.ToActionResult(this);
}
My custom conventions return a special type of problem/details object called ExtendedProblemDetailsResponse when the result is invalid.
When I use the above endpoint, the custom conventions are not invoked.
When I change the endpoint to this
[HttpPut("{salesAgentId}")]
[TranslateResultToActionResult]
public async Task<Result> CreateProspect([FromRoute] Guid salesAgentId,
[FromBody] CreateProspectRequest request,
CancellationToken cancellationToken = default)
{
var result = await handler.CreateAsync(request.ToCommand(salesAgentId,
timeProvider.GetUtcNow()),
cancellationToken);
return result;
}
The result custom conventions start working and I get the customized ExtendedProblemDetailsResponse. Notice that in this version I do not return ActionResult I return Ardalis.Result and I do not call the ToActionResult(this).
There are 2 questions I have
- Is this intended behavior?
- If it is intended behavior, how can I customize what action result to return when I use the explicit
ToActionResult(this) extension method instead of relying on the automatic conversion using the TranslateResultToActionResult.
Further, (this is probably a separate issue). When we use the automatic version where instead of returning ActionResult we return the Ardalis.Result certain filters and middleware stop working which rely on the fact that the controller returns ActionResult. So, I really would prefer using the manual version and be able to customize the returned action result. And if the library does not support that, I can roll my own version of the mapper will do what I need.
I have the following custom conventions configuration in my ASP.NET WebAPI project
Program.csfileI have the following endpoint
My custom conventions return a special type of problem/details object called
ExtendedProblemDetailsResponsewhen the result is invalid.When I use the above endpoint, the custom conventions are not invoked.
When I change the endpoint to this
The result custom conventions start working and I get the customized
ExtendedProblemDetailsResponse. Notice that in this version I do not returnActionResultI returnArdalis.Resultand I do not call theToActionResult(this).There are 2 questions I have
ToActionResult(this)extension method instead of relying on the automatic conversion using theTranslateResultToActionResult.Further, (this is probably a separate issue). When we use the automatic version where instead of returning
ActionResultwe return theArdalis.Resultcertain filters and middleware stop working which rely on the fact that the controller returnsActionResult. So, I really would prefer using the manual version and be able to customize the returned action result. And if the library does not support that, I can roll my own version of the mapper will do what I need.