In einem ASP.NET ASMX WebMethod, das JSON antwortet, kann ich beide eine Ausnahme auslösen & den HTTP-Antwortcode festlegen? Ich dachte, wenn ich eine HttpException warf, würde der Statuscode entsprechend eingestellt werden, aber es kann den Dienst nicht dazu bringen, mit irgendetwas außer einem Fehler von 500 zu antworten.Kann ich den HTTP-Antwortcode festlegen und eine Ausnahme für einen ASMX-JSON-Dienst auslösen?
Ich habe versucht, die folgenden:
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public void TestWebMethod() {
throw new HttpException((int)HttpStatusCode.BadRequest, "Error Message");
}
auch:
[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;
}
}
Diese beiden reagieren mit 500.
Vielen Dank.
Hat immer sein? –
Ich fürchte nicht. – Markus