In ASP.NET ASMX WebMethod that responds with JSON, can I get around the exception and set the HTTP response code? I thought that if I selected HttpException, the status code would be set accordingly, but it would not be able to force the service to respond with anything but a 500 error.
I tried the following:
[WebMethod] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public void TestWebMethod() { throw new HttpException((int)HttpStatusCode.BadRequest, "Error Message"); }
also:
[WebMethod] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public void TestWebMethod() { try { throw new HttpException((int)HttpStatusCode.BadRequest, "Error Message"); } catch ( HttpException ex ) { Context.Response.StatusCode = ex.GetHttpCode(); throw ex; } }
These answers answer at 500.
Many thanks.
Markus
source share