0

I am presently encountering an issue with catching errors in ASP.NET's JSON serialization middleware. Specifically, with the following code.

        var apiEntity = ModelSerializer.ToApi(entity);
        try
        {
            var ok = Ok(apiEntity);
            return ok;
        }
        catch
        {
            return BadRequest(new ApiError { Message = "Error" });
        }

All runs fine. However, the 'catch' block is never activated, and the wrong error propagates.

I am getting this error:

System.ArgumentException: The JSON value of length 733746981 is too large and not supported.

I want to catch this error so that the correct error is sent. How can I catch this error in the middleware?

3
  • 1
    Is ToApi an async method? Irrespective to that, are you sure putting 700 MB of data in a json is the right thing to do? Commented May 3, 2024 at 23:17
  • @GSerg No, ToApi is not async. It's probably not the right thing to do, and it doesn't work to send it through ASP.Net. However, I'm unsure of how to detect Json being too large for ASP.Net. Is there a specific max size for ASP.Net responses? Commented May 6, 2024 at 20:07
  • The (configurable) max size for ASP.NET responses is way lower than that by default. What you are hitting is the max size for the System.Text.Json. See github.com/dotnet/runtime/issues/30746. Commented May 6, 2024 at 20:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.