我正在创建我的第一个ASP.NET MVC2项目,使用客户端的jquery和ajax。
现在,当我进行Q.A测试并添加一些测试异常,从我的控制器将其传递给json,类似于这样:
Controller:
public JsonResult PopulateDetails(int id)
{
ProductServiceClient client = new ProductServiceClient();
try
{
if (id == 0)
{ throw new Exception("TEST ERROR."); }
string name= "test";
List<Product> product= client.GetPriductbyName(name);
return Json(product);
}
catch (Exception ex)
{
throw ex;
}
}
Jquery-Ajax脚本:
$.ajax({
type: "POST",
url: url_ ,
data: search,
success: function(data) { // data: Passed (Records) from the PopulateDetails controller
displayDetails(data); // after routing on Populate Details converts the data to table
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("error: " + XMLHttpRequest.responseText);
},
dataType: 'json'
});
我注意到它会返回以下默认错误信息:
---------------------------
Windows Internet Explorer
---------------------------
error: <html>
<head>
<title>TEST ERROR.</title>
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:"Lucida Console";font-size: .9em}
.marker {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
</style>
</head>
<body bgcolor="white">
<span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>
<h2> <i>TEST ERROR.</i> </h2></span>
<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
<b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
<br><br>
<b> Exception Details: </b>System.Exception: TEST ERROR.<br><br>
<b>Source Error:</b> <br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>
Line 64: catch (Exception ex)
Line 65: {
<font color=red>Line 66: throw ex;
</font>Line 67: }
Line 68: }</pre></code>
</td>
</tr>
</table>
<br>
---------------------------
OK
---------------------------
现在如何将异常中的错误信息整齐地显示出来,而不是整个 HTML 错误信息。类似于显示警告消息框 alert("TEST ERROR") 的简单方式。
有更简单的方法吗?
我看到这在 asp.net 上链接起来很好 How Do I Handle jQuery Errors and Display them on the Client?,但我找不到 AjaxResponseViewModel() 的声明位置。