I'm making a call to a web API controller from ASP code behind. It's a web API and not just a normal code because there is a desktop client that needs to call it as well. The call works fine as an HTTP post on the desktop client. However, on the ASP side I'm getting an ArgumentNullException on the Reqeust.CreateResponse() line of the controller. What is causing this error?
For what it's worth, everything else in the controller is actually functioning as intended. It's just that the asp page does not refresh after the api call like it's supposed to because the error occurs.
This is the exception:
Exception thrown: 'System.ArgumentNullException' in System.Web.Http.dll
Additional information: Value cannot be null.
Message: Value cannot be null. Parameter name: request
Here is a simplified view of my code:
API Controller
public class ImageUploadServiceController : ApiController
{
public ImageUploadServiceController() { }
[HttpPost]
public HttpResponseMessage UploadImage([FromBody] ImageUploadMessage data)
{
// Do some logic here
Request.CreateResponse<string>(HttpStatusCode.OK, String.Format("Upload successful!"));
}
}
ASP code behind
protected void btn_Click(object sender, ImageClickEventArgs e)
{
ImageUploadMessage req = new ImageUploadMessage(){ //Data };
var config = new HttpConfiguration();
ImageUploadServiceController controller = new ImageUploadServiceController();
controller.UploadImage(req);
}