To have the control over where this logic is applied, do not register middleware/filters for all of the apps in Startup.
Do Controller attribute and code like this:
`public class CustomObjectResultFilter : IAsyncResultFilter
{
public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
if (context.Result is ObjectResult objectResult)
{
var executor = context.HttpContext.RequestServices.GetRequiredService();
[Route("api/[controller]")]
[ApiController]
public class MyController : ControllerBase
{
[HttpGet("endpoint1")]
[ServiceFilter(typeof(CustomObjectResultFilter))]
public IActionResult Endpoint1()
{
var data = new { Message = "This is endpoint 1" };
return Ok(data);
}
[HttpGet("endpoint2")]
public IActionResult Endpoint2()
{
var data = new { Message = "This is endpoint 2" };
return Ok(data);
}
To have the control over where this logic is applied, do not register middleware/filters for all of the apps in Startup.
Do Controller attribute and code like this:
`public class CustomObjectResultFilter : IAsyncResultFilter { public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { if (context.Result is ObjectResult objectResult) { var executor = context.HttpContext.RequestServices.GetRequiredService();
}
[Route("api/[controller]")] [ApiController] public class MyController : ControllerBase { [HttpGet("endpoint1")] [ServiceFilter(typeof(CustomObjectResultFilter))] public IActionResult Endpoint1() { var data = new { Message = "This is endpoint 1" }; return Ok(data); }
}`