0

I'm working on ASP.Net boilerplate service project . I want to send a custom exceptions . I implemented

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
    public sealed class FriendlyError : ExceptionFilterAttribute, IExceptionFilter
    {
        private readonly HttpStatusCode StatusCode;

        public FriendlyError(HttpStatusCode statusCode = HttpStatusCode.InternalServerError)
        {
            StatusCode = statusCode;
        }

        public override void OnException(ExceptionContext context)
        {
            if (context.Exception == null) return;

            context.HttpContext.Response.StatusCode = (int)StatusCode;
            context.HttpContext.Response.ContentType = "multipart/form-data;application/json";

            context.Result = new JsonResult(CreateErrorResult(context.Exception), new JsonSerializerSettings
            {
                Formatting = Formatting.Indented,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                ContractResolver =  new CamelCasePropertyNamesContractResolver()
            });
            base.OnException(context);
        }
}

handle exceptions , and put the annotation on the controllers endpoint. but it sends 406 (Not Acceptable) message to the client .

2 Answers2

0

That is not unusual, You have got error 406 Not Acceptable because you are returning an error not expected in the list of acceptable values defined in Accept-Charset and Accept-Language

See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406 for more details.

I think you need to add something like :

context.HttpContext.Response.Charset = CharSet.Unicode;

and

context.HttpContext.Response.ContentType should be set based on type of response, generic "multipart/form-data;application/json" for every request might not be correct.

Pranav Singh
  • 17,079
  • 30
  • 77
  • 104
0

The MIME media type for JSON text is application/json. So change the content type.

Recommendation;

According to me you should check the result type of exception thrower action. If you always send json result, you might ignore other return types. ABP has UserFriendlyException which is neat! Why don't you use that?

Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55