本文介绍了从另一个API调用ASP.NET web api的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从外部控制器方法1调用api方法2,但它会像下面一样抛出异常。请帮助我解决这个问题吗?

如果我调用直接方法然后它工作正常。那么为什么它不是从方法1调用并像下面那样抛出异常。



{message:发生错误。, exceptionMessage:值不能为null。\\\\\\\\\\\\\\\\\\\\\请求,HttpStatusCode statusCode,T值,......}








方法1:


[路线(api / external / vehicles / update)]

公共异步任务< httpresponsemessage> ExternalCustomerUpdateVehicle(VehicleRequest) vehicleRequest)

{

VehicleController VCobj = new VehicleController();

返回等待VCobj.CustomerUpdateVehicle(vehicleRequest);

}





方法2:


[路线(api) / vehicles / update)]

公共异步任务< htt presponsemessage> CustomerUpdateVehicle(VehicleRequest vehicleRequest)

{

//代码部分

返回this.Request.CreateResponse(HttpStatusCode.Created,responseText);

}



我尝试过:



尝试这样但不成功。

HttpResponseMessage结果=等待VCobj.CustomerUpdateVehicle(vehicleRequest);

if(result.IsSuccessStatusCode)

{

返回结果;

}

else

{

String responseText =外部客户更新车辆呼叫失败;

返回this.Request.CreateResponse(HttpStatusCode.Created,responseText);

}

I want to call api method 2 from external controller method 1 but it throw me exception like below.please help me what is the solution of this?
if i call direct method to then it working properly. so why it is not calling from method-1 and throw me exception like below.

{"message":"An error has occurred.","exceptionMessage":"Value cannot be null.\r\nParameter name: request","exceptionType":"System.ArgumentNullException","stackTrace":" at System.Net.Http.HttpRequestMessageExtensions.CreateResponse[T](HttpRequestMessage request, HttpStatusCode statusCode, T value, ......}




Method 1:

[Route("api/external/vehicles/update")]
public async Task<httpresponsemessage> ExternalCustomerUpdateVehicle(VehicleRequest vehicleRequest)
{
VehicleController VCobj = new VehicleController();
return await VCobj.CustomerUpdateVehicle(vehicleRequest);
}


Method 2:

[Route("api/vehicles/update")]
public async Task<httpresponsemessage> CustomerUpdateVehicle(VehicleRequest vehicleRequest)
{
//code part
return this.Request.CreateResponse(HttpStatusCode.Created, responseText);
}

What I have tried:

try like this but not succeed.
HttpResponseMessage result = await VCobj.CustomerUpdateVehicle(vehicleRequest);
if(result.IsSuccessStatusCode)
{
return result;
}
else
{
String responseText = "External Customer update Vehicle Calling Fail";
return this.Request.CreateResponse(HttpStatusCode.Created, responseText);
}

推荐答案


这篇关于从另一个API调用ASP.NET web api的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 05:20