gpt4 book ai didi

c# - 返回Task 方法的JSON错误消息

转载 作者:行者123 更新时间:2023-12-03 08:24:05 28 4
gpt4 key购买 nike

我创建了一个ASP.NET Web API,它将调用Java Web服务并以JSON显示一些结果。当Java Web服务未运行时,我收到以下错误消息:

{"Message":"An error has occurred.","ExceptionMessage":"The method or operation is not implemented.","ExceptionType":"System.NotImplementedException","StackTrace":" at ApiClientJavaWebService.Models.RestfulClient.InternalServerError(Exception exception) in C:\Users\attsuap1\Desktop\Server Project\ApiClientJavaWebService\ApiClientJavaWebService\Models\RestfulClient.cs:line 43\r\n at ApiClientJavaWebService.Models.RestfulClient.d__3.MoveNext() in C:\Users\attsuap1\Desktop\Server Project\ApiClientJavaWebService\ApiClientJavaWebService\Models\RestfulClient.cs:line 36\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\r\n at ApiClientJavaWebService.Controllers.AdditionController.d__1.MoveNext() in C:\Users\attsuap1\Desktop\Server Project\ApiClientJavaWebService\ApiClientJavaWebService\Controllers\AdditionController.cs:line 20\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Threading.Tasks.TaskHelpersExtensions.d__3`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.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
{
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
}
}

关于c# - 返回Task <IHttpActionResult>方法的JSON错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47863073/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com