Home > front end >  Add property to all responses in asp.net core
Add property to all responses in asp.net core

Time:04-09

I have an API with multiple endpoints. I'd like to add a property to all endpoint responses, without adding it to each endpoint response model individually.

Ex:

public class MyClass
{
    public string MyProperty { get; set; } = "Hello";
}

public class MyOtherClass
{
    public string MyOtherProperty { get; set; } = "World";
}

public class MyController : ControllerBase
{
    [HttpPost]
    public async Task<ActionResult<MyClass>> EndpointOne(POSTData data)
    {
        // implementation omitted
    }

    [HttpPost]
    public async Task<ActionResult<MyOtherClass>> EndpointTwo(POSTOtherData otherData)
    {
        // implementation omitted
    }
}

Calling either endpoint returns a JSON representation of MyClass or MyOtherClass as appropriate - i.e.

{ "MyProperty":"Hello" } or { "MyOtherProperty":"World" }

I want to add a property, say a string ApiName, to all endpoints in the API, so that the result of the above code would be either (as appropriate)

{ "MyProperty":"Hello", "ApiName":"My awesome API" } 

or

{ "MyOtherProperty":"World", "ApiName":"My awesome API" }

Is there a way to hook into the JSON-stringified result just before returning and add a top-level property like that? If so, I presume I'd have to wire it up in startup.cs, so I've been looking at app.UseEndpoints(...) methods, but haven't found anything that's worked so far. Either it's not added the property, or it's replaced the original result with the new property.

Thanks in advance!

CodePudding user response:

Use Newtonsoft.Json in your net web api

Register a custom contract resolver in Startup.cs:

builder.Services.AddControllers()
    .AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = CustomContractResolver.Instance);

The implementation:

public class CustomContractResolver : DefaultContractResolver {
    public static CustomContractResolver Instance { get; } = new CustomContractResolver();

    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        var properties = base.CreateProperties(type, memberSerialization);
        // add new property
        ...
        properties.Add(newProp);
        return properties;
    }}

See more Json.net Add property to every class containing of a certain type

CodePudding user response:

For a simple case like you suggest, I think you can add a base class with the shared property. Should work for both XML and JSON.

public class MyApiClass
{
    public string ApiName => "MyAwesomeApi";
}

public class MyClass : MyApiClass
{
    public string MyProperty { get; set; } = "Hello";
}

public class MyOtherClass : MyApiClass
{
    public string MyOtherProperty { get; set; } = "World";
}

public class MyController : ControllerBase
{
    [HttpPost]
    public async Task<ActionResult<MyClass>> EndpointOne(POSTData data)
    {
        // implementation omitted
    }

    [HttpPost]
    public async Task<ActionResult<MyOtherClass>> EndpointTwo(POSTOtherData otherData)
    {
        // implementation omitted
    }
}
  • Related