繁体   English   中英

更改Web API中的错误响应

[英]Change Error response in web API

我在Web Api应用程序中使用基于令牌的身份验证。 在登录请求中,如果用户输入了错误的凭据,则它会以此格式返回响应错误。

GrantResourceOwnerCredentials方法中

if (user == null)
    {
      context.SetError("The email or password is incorrect.");
      return;
    }

哪个回复响应为

 {
      "error": "InvalidLogin",
      "error_description": "The email or password is incorrect."
    }

在我的应用程序中,所有错误响应都以此格式返回

{
  "message": "Some Internal server error occurred."
}

那么如何才能将web api的错误响应更改为

    {
      "message": "The email or password is incorrect."
    }

编辑:尝试这个

string jsonString = "{\"message\": \"The email or password is incorrect.\"}";
context.SetError(new string(' ',jsonString.Length-12)); 
context.Response.StatusCode = 400;
context.Response.Write(jsonString);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM