Reputation: 369
I'm trying to set the location header on a web api post method if the item was inserted successfully, but problem is if I try to call that api action from a mvc controller it gives me an exception on Url.Link saying parameter 'request' is missing. Is there a way to generate the url without needing the request?
Here is the web api post code -
public HttpResponseMessage Post (Product product)
{
...
if (addedSuccessfully)
{
var response = new HttpResponseMessage {StatusCode = HttpStatusCode.Created};
var uri = Url.Link("DefaultApi", new {id = product.ProductId});
if (uri != null)
response.Headers.Location = new Uri(uri);
return response;
}
return new HttpResponseMessage {StatusCode = HttpStatusCode.Conflict};
}
Here is how I am calling it
_productController.Post(product);
Upvotes: 0
Views: 261
Reputation: 6793
Ideally, you should not create web API controllers and start invoking actions on them unless you are unit testing. Controller's have context that gets initialized by the web API system when they are created internally.
If you could explain your scenario as in what you are doing with the HttpResponseMessage that gets returned from web API, may be I could present an alternative to solve it.
Upvotes: 1