【发布时间】:2012-11-12 20:13:16
【问题描述】:
在 .NET 3.5 中,我有以下代码:
[WebService(Namespace = "http://kitchenpc.com/schemas/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class KitchenPC : System.Web.Services.WebService
{
[WebMethod]
public LogonResult Logon(string username, string password)
{
//If username and password are not valid...
throw new InvalidUsernameOrPasswordException();
}
}
当我调用它时,如果我传入了无效的用户名和密码,InvalidUsernameOrPasswordException 将被抛出,我可以通过查看 error.get_exceptionType() 在 Javascript 中捕获异常。这是因为 Web 服务会将异常信息序列化为 JSON。
但是,一旦我升级到 .NET 4.5,它就坏了。现在,当我传入无效的用户名和密码时,我会收到 HTTP 响应:
HTTP/1.1 500 Internal Server Error
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
jsonerror: true
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sat, 24 Nov 2012 22:42:33 GMT
Content-Length: 91
{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}
基本上,异常类型会丢失并替换为通用错误消息。
是什么导致了这种行为的改变,还有没有办法以 JSON 格式返回异常信息?几乎我的整个 API 都是为依赖这种行为而设计的。
【问题讨论】:
标签: c# asp.net .net web-services asp.net-ajax