Skip to content

Allow Custom JsonSerializerOptions for Minimal Actions #35904

@Kahbazi

Description

@Kahbazi

Background and Motivation

Add ability to have custom JsonSerializerOptions for serializing and deserializing in minimal actions.

Proposed API

namespace Microsoft.AspNetCore.Builder
{
     public static class DelegateEndpointRouteBuilderExtensions
     {
+        public static DelegateEndpointConventionBuilder Map(
+            this IEndpointRouteBuilder endpoints,
+            RoutePattern pattern,
+            Delegate handler,
+            JsonSerializerOptions options)
    }
}

namespace Microsoft.AspNetCore.Http
{
     public sealed class RequestDelegateFactoryOptions
     {
+      public JsonSerializerOptions JsonSerializerOptions { get; init; }
     }
}

Usage Examples

var jsonOptions = new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};

app.Map("\action", () => MyAction(), jsonOptions);

Alternative Designs

This could also be added via Endpoint metadata in order to prevent all extra overloads (MapGet, MapPost, ...), but the downside is that it would add an extra fetch from Endpoint.Metadata for serializing and deserializing.

public static class DelegateEndpointRouteBuilderExtensions
{
+   public static DelegateEndpointConventionBuilder WithJsonOptions(
+       this DelegateEndpointConventionBuilder builder,
+       JsonSerializerOptions options)
}

Risks

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-actionsController-like actions for endpoint routing

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions