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 .