无法在Web Api 2中收到BadRequest的错误消息

昆塔尔

在我的一种方法中,我必须IHttpActionResult在Web API 2中从那里返回。成功后,它Ok()要么返回要么返回BadRequest()一条消息。BadRequest以以下方式设置消息但是问题是我无法从被调用的方法中检索错误消息。我使用的是Visual Studio 2013 Update 4,版本为MVC 5。

return BadRequest("Error!  Error adding the device");

请帮忙。提前致谢。

昆塔尔

经过研究和研究,我找到了解决方案。由于这是Web API的一种方法,因此我已经通过网站对其进行了调用。我已经按照以下方式调用了它RequestUrl,该Web API方法的URL在哪里如果方法中有参数,则必须将其写入查询字符串中。

HttpResponseMessage Response = null;
Response = client.GetAsync(RequestUrl).Result;
var stream = Response.Content.ReadAsStreamAsync().Result;

// convert stream to string
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();

我们还可以使用以下代码获得响应类型。

Response.ReasonPhrase

并以这种方式我可以的消息BadRequestOk答对了 !!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章