【发布时间】:2017-05-03 14:36:34
【问题描述】:
我是 Web Api 2 的新手,如果我点击 Exception ,我想显示一些消息,我知道 VOID 是非返回类型,我尝试使用 HttpResponseMessage 但它不起作用或显示“错误消息”,这是显示消息的任何方式吗?
我的代码如下
public void Post(int id)
{
string result = string.Empty;
try
{
//some code
}
catch (Exception ex)
{
HttpResponseMessage response2 = Request.CreateResponse(HttpStatusCode.BadRequest, "Error message");
ErrId = 999;
}
}
【问题讨论】:
-
如果你遇到了异常,那么 ex.Message 应该会告诉你你需要什么。
-
@Wheels73 可以在异常部分显示一些示例?
-
只需将“错误消息”替换为 ex.Message。
-
使用
public void Post(int id)似乎有点可疑。这看起来像一个名为“post”的 GET 方法。你确定你真的调用了正确的函数吗? -
是什么阻止您返回您在
catch块中创建的response2结果?如果成功,返回 Created() 或 Ok()。
标签: c# asp.net-web-api asp.net-web-api2