返回任务<IHttpActionResult>方法的JSON错误消息

我创建了一个ASP.NET Web API,它将调用java web服务并在JSON中显示一些结果。当java web服务未运行时,我收到错误消息:返回任务<IHttpActionResult>方法的JSON错误消息

{“Message”:“发生错误。”,“ExceptionMessage”:“方法或操作未实现。”, C:\ Users \ attsuap1 \ Desktop \ Server Project \ ApiClientJavaWebService \ ApiClientJavaWebService \ Models \ RestfulClient.cs中的ApiClientJavaWebService.Models.RestfulClient.InternalServerError(异常例外)中的“ExceptionType”:“System.NotImplementedException”,“StackTrace”在C:\ Users \ attsuap1 \ Desktop \ Server Project \ ApiClientJavaWebService \ ApiClientJavaWebService \ Models \ RestfulClient.cs中的ApiClientJavaWebService.Models.RestfulClient.d__3.MoveNext()中的第43行\ r \ n:第36行\ r \ n ---从抛出异常的上一个位置结束堆栈跟踪--- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)中的\ r \ n \ System.Runtim中的\ r \ n e.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\ r \ n在ApiClientJavaWebService.Controllers.AdditionController.d__1.MoveNext()在C:\ Users \ attsuap1 \ Desktop \ Server Project \ ApiClientJavaWebService \ ApiClientJavaWebService \ Controllers \ AdditionController.cs:第20行\ r \ n ---从以前位置抛出异常的堆栈跟踪结束--- \ r \ n在System.Runtime.CompilerServices .TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n在System.Threading.Tasks.TaskHelpersExtensions.d__3`1.MoveNext()\ r \ n System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n ---从以前位置抛出异常的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)上的\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在System.Web.Http.Controllers.ApiControl lerActionInvoker.d__0.MoveNext()\ r \ n ---从之前的位置抛出异常的堆栈跟踪结束--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\ r \ n下System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n ---以前位置的堆栈跟踪结束,其中异常在System.Web的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n处,System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n处抛出--- \ r \ n。 Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()“}

我要如何改变这种去显示为{"The server is not running."}错误信息?

这里是我的代码:

AdditionClient.cs

public class RestfulClient 

{

private static HttpClient client;

private static string BASE_URL = "http://localhost:8080/";

static RestfulClient()

{

client = new HttpClient();

client.BaseAddress = new Uri(BASE_URL);

client.DefaultRequestHeaders.Accept.Add(

new MediaTypeWithQualityHeaderValue("application/json"));

}

public async Task<string> addition(int firstNumber, int secondNumber)

{

try

{

var endpoint = string.Format("addition/{0}/{1}", firstNumber, secondNumber);

var response = await client.GetAsync(endpoint);

return await response.Content.ReadAsStringAsync();

}

catch (Exception e)

{

}

return null;

}

}

AdditionController.cs

public class AdditionController : ApiController 

{

private RestfulClient restfulClient = new RestfulClient();

public async Task<IHttpActionResult> Get(int firstNumber, int secondNumber)

{

var result = await restfulClient.addition(firstNumber, secondNumber);

var resultDTO = JsonConvert.DeserializeObject<Temp>(result);

return Json(resultDTO);

}

}

有人请你帮帮我。提前谢谢你。

回答:

我已经达到了我想要的。我意识到try catch方法应该在控制器类中而不是AdditionClient.cs类中。

这里是我的编辑类:

AdditionClient.cs

public class RestfulClient 

{

private static HttpClient client;

private static string BASE_URL = "http://localhost:8080/";

static RestfulClient()

{

client = new HttpClient();

client.BaseAddress = new Uri(BASE_URL);

client.DefaultRequestHeaders.Accept.Add(

new MediaTypeWithQualityHeaderValue("application/json"));

}

public async Task<string> addition(int firstNumber, int secondNumber)

{

var endpoint = string.Format("addition/{0}/{1}", firstNumber, secondNumber);

var response = await client.GetAsync(endpoint);

return await response.Content.ReadAsStringAsync();

}

}

AdditionController.cs(聚焦在尝试捕捉部分)

 try 

{

var result = await restfulClient.addition(firstNumber, secondNumber);

var resultDTO = JsonConvert.DeserializeObject<Temp>(result);

return Json(resultDTO);

}

catch (Exception e)

{

var result = @"Server is down";

return Ok(new { ErrorMessage = result });

}

,这将是结果当服务器没有运行时,在浏览器上显示的是{"ErrorMessage":"Server is down"}

回答:

我想,你不能管理抛出你想要拉动数据的服务器的异常消息。那么,为什么不过滤这个异常呢?

 try 

{

var endpoint = string.Format("addition/{0}/{1}", firstNumber, secondNumber);

var response = await client.GetAsync(endpoint);

return await response.Content.ReadAsStringAsync();

}

catch (Exception e)

{

if (e.InnerException.Message == "The method or operation is not implemented")

{

//Do something

}

}

以上是 返回任务&lt;IHttpActionResult&gt;方法的JSON错误消息 的全部内容, 来源链接: utcz.com/qa/261188.html

回到顶部