How comes that a custom ExceptionHandler is never called and instead a standard response When Call HTTP Methods Post
This my code
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
//Handler Custom Exception
config.Services.Replace(typeof(IExceptionHandler), new CustomExceptionHandler());
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new JsonOutputDateTime());
// Web API routes
config.MapHttpAttributeRoutes();
// Remove the XML formatter (Json Only)
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/v1/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
ExceptionHandler.cs
public class CustomExceptionHandler : System.Web.Http.ExceptionHandling.ExceptionHandler
{
public override Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
{
SystemExceptionReturn returnObj = new SystemExceptionReturn();
returnObj.responseCode = "xxxx";
returnObj.responseMessage = "System Error";
var response = context.Request.CreateResponse(HttpStatusCode.OK, returnObj);
context.Result = new ResponseMessageResult(response);
return base.HandleAsync(context, cancellationToken);
}
public virtual bool ShouldHandle(ExceptionHandlerContext context)
{
return true;
}
private class TextPlainErrorResult : IHttpActionResult
{
public HttpRequestMessage Request { get; set; }
public string Content { get; set; }
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
HttpResponseMessage response =
new HttpResponseMessage(HttpStatusCode.InternalServerError);
response.Content = new StringContent(Content);
response.RequestMessage = Request;
return Task.FromResult(response);
}
}
}
Global.asax.xs
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
my Controller
public class gController : ApiController
{
[HttpPost]
public bool haha(int id)
{
bool res = false;
try
{
int ans = id / 0;
}
catch (Exception ex)
{
throw ex;
}
return res;
}
}
This response when I call localhost:xxxx/api/v1/g/haha
{
"Message": "An error has occurred.",
"ExceptionMessage": "Attempted to divide by zero.",
"ExceptionType": "System.DivideByZeroException"}
but when i change HttpPost to HttpGet it' working for me.
Please someone help me
Sorry for my English
UPDATE
I found when test in localhost is't not working but when Deploy to IIS it' working Thank you so much for help
To Send POST request to localhost:xxxx/api/v1/g/haha
URL if you change Id parameter to be received [FromBody] it will work.
[HttpPost]
public bool haha([FromBody]int id)
{
bool res = false;
try
{
int ans = id / 0;
}
catch (Exception ex)
{
throw ex;
}
return res;
}
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.