I'm using the Request.CreateResponse method in asp.net web api (.net 4.5) to attempt to return a response after a PUT op.
documentSession.SaveChanges();
// Http PUT response must return URI to created / updated resource
// Line below throws exception
var response = Request.CreateResponse<T>(HttpStatusCode.Created, resource);
response.Headers.Location = new Uri(Request.RequestUri, pathToResource);
return response;
My unit tests currently use this method to setup the request context:
public static void SetupControllerContext(BaseApiController controller, HttpMethod method)
{
controller.Request = new HttpRequestMessage(method, "http://localhost");
//controller.Request.Headers.Add("Accept", "application/json");
var configuration = new System.Web.Http.HttpConfiguration(new System.Web.Http.HttpRouteCollection());
controller.Request.Properties["MS_HttpConfiguration"] = configuration;
}
The exception (thrown on the Request.CreateResponse method above) is: The format of value 'application/json; charset=utf-8' is invalid.
Stacktrace:
at System.Net.Http.Headers.MediaTypeHeaderValue.CheckMediaTypeFormat(String mediaType, String parameterName)
at System.Net.Http.Headers.MediaTypeHeaderValue..ctor(String mediaType)
at System.Net.Http.ObjectContent.BuildHeaderValue(String mediaType)
at System.Net.Http.ObjectContent`1..ctor(T value, MediaTypeFormatter formatter, String mediaType)
at System.Net.Http.HttpRequestMessageExtensions.CreateResponse[T](HttpRequestMessage request, HttpStatusCode statusCode, T value, HttpConfiguration configuration)
at System.Net.Http.HttpRequestMessageExtensions.CreateResponse[T](HttpRequestMessage request, HttpStatusCode statusCode, T value)
at SoftModel.UI.Html.Controllers.BaseApiController.SaveChanges[T](T resource, String pathToResource) in c:\Users\Us\Documents\Visual Studio 2010\Projects\SoftModel\SoftModel.UI.Html\Controllers\BaseApiController.cs:line 113
at SoftModel.UI.Html.Controllers.InstanceController.Put(Record record) in c:\Users\Us\Documents\Visual Studio 2010\Projects\SoftModel\SoftModel.UI.Html\Controllers\InstanceController.cs:line 62
at SoftModel.Tests.InstanceApiControllerTests.Put_Valid_Record_Returns_Record_URI() in c:\Users\Us\Documents\Visual Studio 2010\Projects\SoftModel\SoftModel.Tests\InstanceControllerTests.cs:line 69
I've checked the source of the MediaTypeHeaderValue and cannot work out what is wrong?