Web API 'Request' is null

asp.net-mvc, asp.net-web-api, c#

Solution

Use this:

HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.InternalServerError);
message.Content = new ObjectContent(typeof(MessageResponse), "Invalid Size", GlobalConfiguration.Configuration.Formatters.JsonFormatter);
throw new HttpResponseException(message);

Note: you can change "Invalid Size" with any object you may want to return for the user. e.g.:

public ErrorMessage
{
     public string Error;
     public int ErrorCode;
}

ErrorMessage msg = new ErrorMessage();
msg.Error = "Invalid Size";
msg.ErrorCode = 500;

HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.InternalServerError);
message.Content = new ObjectContent(typeof(MessageResponse), msg, GlobalConfiguration.Configuration.Formatters.JsonFormatter);
throw new HttpResponseException(message);

Problem

I'm trying to return an `HttpResponseException` from a POST Web API action using 'Request': ``` throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "File not in a correct size")); ``` When doing so, I get `Value cannot be null. Parameter name: request`. Essentially - Request is null. What am I missing? Thank you

Original source