简体   繁体   中英

How to Override 415 response in ASP.Net Core2.2

in .net core 2.2 ,has a default json with statuscode 415 like

{
   "type":"https://tools.ietf.org/html/rfc7231#section-6.5.13",
   "title":"Unsupported Media Type",
   "status":415,
   "traceId":"8000003e-0001-f500-b63f-84710c7967bb"
}

I don't know how this JSON came from. I follow the example below to rewrite json

But I got a different result,It added a section to the original json . This is my Wireshark Result

HTTP/1.1 415 Unsupported Media Type Transfer-Encoding: chunked

Content-Type: application/problem+json; charset=utf-8 Server:

Microsoft-IIS/10.0 X-Powered-By: ASP.NET Date: Mon, 06 May 2019 09:03:56 GMT

{
   "type":"https://tools.ietf.org/html/rfc7231#section-6.5.13",
   "title":"Unsupported Media Type",
   "status":415,
   "traceId":"8000002c-0002-fb00-b63f-84710c7967bb"
}
{
   "data":"this is custom message"
}

Filter :

public class MediaTypeResouceFilter : Attribute, IResourceFilter
{
    public void OnResourceExecuting(ResourceExecutingContext context)
    {
    }

    public void OnResourceExecuted(ResourceExecutedContext context)
    {
        if (context.HttpContext.Response.StatusCode == 415)
        {
            var jsonString = JsonConvert.SerializeObject(new { data = "this is custom message" });
            byte[] data = Encoding.UTF8.GetBytes(jsonString);
            context.HttpContext.Response.Body.WriteAsync(data, 0, data.Length);
        }
    }
}

I don't know how this JSON came from.

When the [ApiController] attribute is applied to a controller, it enables Problem details for error status codes , which ends up with a built-in action filter being added to the MVC filter pipeline for that controller. This action filter applies to all status-codes >= 400 and produces the JSON response you've described.

It added a section to the original json

When your MediaTypeResouceFilter.OnResourceExecuted code runs, the action filter I've noted above has already written JSON to the body. You write an additional JSON formatted string to the body, which just gets appended and mangles the response to be invalid JSON.

If you want to disable this problem details JSON from ever being written for responses, you can add the following to your Startup.ConfigureServices code to suppress the functionality:

services.AddMvc()
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
    .ConfigureApiBehaviorOptions(options =>
    {
        options.SuppressMapClientErrors = true;
    };

It looks like you are only appending to body, and whatever was written to body before your filter executed, remained in the body.

If you take a look at the documentation for how to implement IResource filter, you can achieve your goal with this line of code, but inside OnResourceExecuting instead:

context.Result = new ContentResult()
{
    Content = jsonString
};

The documentation states that:

You can short-circuit the filter pipeline at any point by setting the Result property on the context parameter provided to the filter method.

if you put that code inside OnResourceExecuting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM