calling this method:
public HttpResponseMessage PostProduct(Product item)
{
item = repository.Add(item);
var response = this.Request.CreateResponse<Product>CreateResponse(HttpStatusCode.Created, item);
string uri = Url.RouteUrl("DefaultApi", new { id = item.Id });
response.Headers.Location = new Uri(uri);
return response;
}
Is causing a compile-time error:
'System.Web.HttpRequestBase' does not contain a definition for 'CreateResponse' and the
best extension method overload 'System.Net.Http.HttpRequestMessageExtensions.CreateResponse<T>
(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, T)' has some invalid arguments.
What am I missing here?
The runtime type of
itemis probably not an instance ofProduct. You should be able to do this:Even if
itemwas an instance ofProduct, the generic<Product>argument is redundant and not necessary. If you used ReSharper, it would tell you that the “(Generic) Type argument specification is redundant”.Update
Does your class extend from
ControllerorApiController? The error should be'System.Net.Http.HttpRequestMessage' does not contain a definition for..., not'System.Web.HttpRequestBase' does not contain a definition for....WebApi controllers should extend from
ApiController, notController. In an MVC controller,this.Requestpoints to an instance ofSystem.Web.HttpRequestBase. In a WebAPI controller,this.Requestpoints to an instance ofSystem.Net.Http.HttpRequestMessage.