return the server generated identifier in response with minimal changes

As far as I know, you could try to use asp.net core action filter as global filter or as attribute to achieve your requirement.

You could get the httpcontext in the OnActionExecuted method and then you could register the filter for specific controller or as global filter.

More details, you could refer to below codes:

Create a action filter:

public class TestActionFilter : IActionFilter
{
    public void OnActionExecuted(ActionExecutedContext context)
    {
        var re =  context.HttpContext.TraceIdentifier;
        context.HttpContext.Response.Headers.Add("TraceIdentifier", re);

    }

    public void OnActionExecuting(ActionExecutingContext context)
    {

 
        
        //throw new NotImplementedException();
    }


}

Add it as attribute in web api controller:

[TypeFilter(typeof(TestActionFilter))]
public class WeatherForecastController : ControllerBase

Or register it as global filter:

public void ConfigureServices(IServiceCollection services)
{
   services.AddControllers(options=>options.Filters.Add(typeof(TestActionFilter)));
}

Result:

enter image description here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top