Skip to content

Using filter (IRouteHandlerFilter) in minimal API throws exception #41464

Closed
@JohnyL

Description

@JohnyL

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Say, I have this simple API:

app.MapPost("/clients", (SearchCriteria search) => Results.Ok())
   .AddFilter<ValidationFilter<SearchCriteria>>();

I use empty validation filter:

public class ValidationFilter<T> : IRouteHandlerFilter where T : class
{
  public async ValueTask<object> InvokeAsync(
    RouteHandlerInvocationContext context,
    RouteHandlerFilterDelegate next)
  {
    return await next(context);
  }
}

SearchCriteria class:

public class SearchCriteria
{
  public int Id { get; set; }
  public string Name { get; set; }
}

Request to this API throws exception:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.NullReferenceException: Object reference not set to an instance of an object.
at lambda_method5(Closure, RouteHandlerInvocationContext)
at ValidationFilter`1.InvokeAsync(RouteHandlerInvocationContext context, RouteHandlerFilterDelegate next) in E:\Projects\TestWebApi\TestWebApi\Program.cs:line 93
at Microsoft.AspNetCore.Http.RequestDelegateFactory.ExecuteObjectReturn(Object obj, HttpContext httpContext)
at Microsoft.AspNetCore.Http.RequestDelegateFactory.<>c__DisplayClass64_2.<b__2>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

7.0.100-preview.3.22179.4

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions