Summary

Filters are MVC construct that allow developers to. Unlike middlewares, filters run after the routing middleware and have access to additional data that middlewares don’t. While middlewares operate on the HttpContext, filters have access MVC-specific constructs (such as HttpContext, RotueData, ActionDescriptor, and ModelState) that they can operate on.

Filters, and particularly ActionFilters , are heavily tied to action and controllers concepts in MVC and there are no parallels that exist for endpoints. This design doc outlines an approach for adding support for filters on endpoints that are registered as route handlers. Some general notes about the approach outlined here:

Goals

Non-goals

Risks/Unknowns

Proposed Design Walkthrough

  1. The user implements UnsupportedContentTypeFilter by extending the IEndpointFilter interface.
public class UnsupportedContentTypeFilter : IEndpointFilter
{
	public override ValueTask<object> RunAsync(IEndpointFilterContext context, Func<IEndpointFilterContext, ValueTask<object?>> next)
	{
			// Assume last argument is some Boolean flag
			if (context.Parameters.Last())
			{
				var result = await next(context);
				if (result.ContentType != "application/json")
				{
					return new UnsupportedMediaTypeResult();
				}
				return result;
			}
	} 
}