Can I Set the HTTP Response Code & Throw an Exception on an ASMX JSON Service? Can I Set the HTTP Response Code & Throw an Exception on an ASMX JSON Service? asp.net asp.net

Can I Set the HTTP Response Code & Throw an Exception on an ASMX JSON Service?


Change your code to this:

[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();        // See Markus comment        // Context.Response.StatusDescription("Error Message");        // Context.Response.StatusDescription(ex.Message); // exception message        // Context.Response.StatusDescription(ex.ToString()); // full exception    }}

Basically you can't, that is, when throwing an exception the result will always be the same 500.