Json 如何让MVC操作返回错误状态
下面是我的场景,我从数据库中获取数据,在控制器操作中尝试/捕获它,并希望将代码和错误消息传递回我的jsJson 如何让MVC操作返回错误状态,json,error-handling,Json,Error Handling,下面是我的场景,我从数据库中获取数据,在控制器操作中尝试/捕获它,并希望将代码和错误消息传递回我的js //in my action [HttpPost] public JsonResult GetWorkorderDetails(string folderno) { try{ //do some stuff - fetch the data }catch() { //if error, I would like to set the
//in my action
[HttpPost]
public JsonResult GetWorkorderDetails(string folderno)
{
try{
//do some stuff - fetch the data
}catch() {
//if error, I would like to set the JsonResult to return bad status and error message
}
}
//in my js
if(response.status == 400){
//if error, output the message response.responseText and return
}
有没有办法在catch语句中的操作中将我的响应状态设置为400?我会将返回类型更改为generic ActionResult,并在需要时返回新的HttpStatusCodeResult 试试smth。如
返回新的HttpStatusCodeResult(400,“发生异常”)代码>嗨,你能把你的评论作为回答吗?