json - 如何获得MVC Action 以返回错误状态

标签 json error-handling

这是我的场景,我从数据库中获取数据,在 Controller 操作中尝试/捕获它,并希望将代码和错误消息传递回我的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 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?

最佳答案

我将返回类型更改为通用ActionResult,并在需要时返回新的HttpStatusCodeResult。

关于json - 如何获得MVC Action 以返回错误状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26679239/

相关文章:

python - 字符串索引在python中为什么必须是整数?

php - 导入 XML 并跳过错误 (PHP)

SQLSTATE 58004,在 DB2 LUW 中的 LISTAGG 中使用 JSON_OBJECT() 中的 NULL 时出现错误 "invalid qnc assigment"

php - jQuery UI 自动完成 - 西类牙字符在本地正确显示但在服务器上不正确

javascript - 向 JSON 解析对象添加方法

java - 使用 Java [json-simple.jar 库] 将名称添加到 JsonArray 或 JsonObject

c# - 将捕获的 ASP.NET 异常写入 EventLog 而不会丢失详细信息

android - 如何从 422 Unprocessable Entity 响应中获取错误消息

bash - 在Bash脚本中捕获被调用命令的所有错误信号

php - 如何将一个 JSON 包含在另一个 JSON 中?