Introducing JustController:
// many using directives
namespace MyWebApiProject.Controllers
{
[Route("api/[controller].[action]")]
[ApiController]
public class JustController : ControllerBase
{
[HttpPost]
public async Task<ActionResult> GetById(/*Model Class*/ body)
{
return StatusCode(/* http status code */, "THIS IS NEEDED VALUE"); // <- Look at this!
}
}
}
We also have this middleware:
// many using directives
public class StatusCodeMiddleware
{
private readonly RequestDelegate _next;
public StatusCodeMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext httpContext)
{
await _next(httpContext);
/*
How to get "THIS IS NEEDED VALUE" from HttpContext here
*/
}
}
public static class StatusCodeMiddlewareExtensions // For convenient use in Startup.cs
{
public static void ConfigureCustomStatusCodeMiddleware(this IApplicationBuilder app)
{
app.UseMiddleware<StatusCodeMiddleware>();
}
}
Startup.cs:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.ConfigureCustomStatusCodeMiddleware();
app.UseRouting();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
How do I get "THIS IS NEEDED VALUE" from HttpContext inside the InvokeAsync() method, inside of StatusCodeMiddleware?
Thanks in advance! I really hope the Stack Overflow audience helps me! I googled the solution to this problem for 2 days so I did not find anything. Sorry for my bad English!