I have a simple ASP.NET MVC 5 (not .NET Core) project in which I want to create a simple middleware like we do in .NET Core:
public class CustomMiddleware
{
internal const string HeaderKey = "RelationId";
private readonly RequestDelegate _next;
public CustomMiddleware(RequestDelegate next)
{
this._next = next;
}
public async Task Invoke(HttpContext context)
{
var id = Guid.NewGuid();
if (context.Request != null)
{
context.Request.Headers.Add(HeaderKey, id.ToString());
}
await this._next.Invoke(context);
}
}
and in the startup we can do app.UseMiddleWare<CustomMiddleware>();
Can we do the same in an ASP.NET MVC 5 project?