public Task<HttpResponseMessage> UpdateOrder()
{
int orderId = Convert.ToInt32(this.Request.GetRouteData().Values["orderId"]);
But this:
public Task<HttpResponseMessage> UpdateOrder(int orderId)
{
//int orderId = Convert.ToInt32(this.Request.GetRouteData().Values["orderId"]);
Causes this exception to be returned
"{\"ExceptionType\":\"System.InvalidOperationException\",\"Message\":\"No 'MediaTypeFormatter' is available to read an object of type 'Int32' with the media type 'application\\/vnd.weborder+xml'.\",\"StackTrace\":\" at System.Net.Http.ObjectContent.SelectAndValidateReadFormatter(Boolean acceptNullFormatter)\\u000d\\u000a at System.Net.Http.ObjectContent.ReadAsyncInternal[T](Boolean allowDefaultIfNoFormatter)\\u000d\\u000a at System.Web.Http.ModelBinding.RequestContentReader.ReadWholeBodyAsync(HttpActionContext actionContext, Type modelType)\\u000d\\u000a at System.Web.Http.ModelBinding.RequestContentReader.ReadContentAsync(HttpActionContext actionContext)\\u000d\\u000a at System.Web.Http.ModelBinding.DefaultActionValueBinder.BindValuesAsync(HttpActionContext actionContext, CancellationToken cancellationToken)\\u000d\\u000a at System.Web.Http.ApiController.<>c__DisplayClass3.<ExecuteAsync>b__0()\\u000d\\u000a at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerCon
text, CancellationToken cancellationToken)\\u000d\\u000a at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage request, CancellationToken cancellationToken)\\u000d\\u000a at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\"}"
I am not sure about the route value binding part, but regarding the mediatypeformatter, you have added it as part of the Formatters list on the Configuration object...right?
I doubt this is the case, but I know that for some properties, once they are model bound, they are removed from the contextual elements. However, wrt RouteData, I would think this a bug.
Chris did you add your custom vnd.mediatype names to the supported collection on the Json formatter? At least in preview 6 this error happens because the framework can't find a formatter to deserialize your media type. I solved that by adding all the media
type in my system to the formatter, in my case using reflection (I store the media type on the actions).
chrisortman
Member
38 Points
23 Posts
Binding to route value and CustomMediaTypeFormatter
Feb 17, 2012 10:03 PM|LINK
This works:
public Task<HttpResponseMessage> UpdateOrder() { int orderId = Convert.ToInt32(this.Request.GetRouteData().Values["orderId"]);But this:
public Task<HttpResponseMessage> UpdateOrder(int orderId) { //int orderId = Convert.ToInt32(this.Request.GetRouteData().Values["orderId"]);Causes this exception to be returned
"{\"ExceptionType\":\"System.InvalidOperationException\",\"Message\":\"No 'MediaTypeFormatter' is available to read an object of type 'Int32' with the media type 'application\\/vnd.weborder+xml'.\",\"StackTrace\":\" at System.Net.Http.ObjectContent.SelectAndValidateReadFormatter(Boolean acceptNullFormatter)\\u000d\\u000a at System.Net.Http.ObjectContent.ReadAsyncInternal[T](Boolean allowDefaultIfNoFormatter)\\u000d\\u000a at System.Web.Http.ModelBinding.RequestContentReader.ReadWholeBodyAsync(HttpActionContext actionContext, Type modelType)\\u000d\\u000a at System.Web.Http.ModelBinding.RequestContentReader.ReadContentAsync(HttpActionContext actionContext)\\u000d\\u000a at System.Web.Http.ModelBinding.DefaultActionValueBinder.BindValuesAsync(HttpActionContext actionContext, CancellationToken cancellationToken)\\u000d\\u000a at System.Web.Http.ApiController.<>c__DisplayClass3.<ExecuteAsync>b__0()\\u000d\\u000a at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerCon text, CancellationToken cancellationToken)\\u000d\\u000a at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage request, CancellationToken cancellationToken)\\u000d\\u000a at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\"}"Kiran Challa
Participant
1442 Points
281 Posts
Microsoft
Re: Binding to route value and CustomMediaTypeFormatter
Feb 17, 2012 10:17 PM|LINK
I am not sure about the route value binding part, but regarding the mediatypeformatter, you have added it as part of the Formatters list on the Configuration object...right?
Kiran Challa
panesofglass
Member
730 Points
237 Posts
Re: Binding to route value and CustomMediaTypeFormatter
Feb 17, 2012 11:15 PM|LINK
I doubt this is the case, but I know that for some properties, once they are model bound, they are removed from the contextual elements. However, wrt RouteData, I would think this a bug.
danroth27
Member
174 Points
40 Posts
Microsoft
Re: Binding to route value and CustomMediaTypeFormatter
Feb 17, 2012 11:19 PM|LINK
What does your route table look like? Is the route data for the orderId parameter present?
Senior Program Manager
ASP.NET
SiggiGG
Member
265 Points
105 Posts
Re: Binding to route value and CustomMediaTypeFormatter
Feb 18, 2012 09:30 AM|LINK
Chris did you add your custom vnd.mediatype names to the supported collection on the Json formatter? At least in preview 6 this error happens because the framework can't find a formatter to deserialize your media type. I solved that by adding all the media type in my system to the formatter, in my case using reflection (I store the media type on the actions).
chrisortman
Member
38 Points
23 Posts
Re: Binding to route value and CustomMediaTypeFormatter
Feb 20, 2012 01:29 PM|LINK
Yes
chrisortman
Member
38 Points
23 Posts
Re: Binding to route value and CustomMediaTypeFormatter
Feb 20, 2012 01:30 PM|LINK
I believe so because I am able to manually pull the value out of the GetRouteData.Values dictionary.
This is how I map the route
_config.Routes.MapHttpRoute("UpdateOrderApiUrl", "api/orders/{orderId}", new { controller = "OrdersApi", action = "UpdateOrder" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) });chrisortman
Member
38 Points
23 Posts
Re: Binding to route value and CustomMediaTypeFormatter
Feb 20, 2012 01:31 PM|LINK
I didn't add my media types to the JsonFormatter.
This all worked in preview6 though.
chrisortman
Member
38 Points
23 Posts
Re: Binding to route value and CustomMediaTypeFormatter
Feb 20, 2012 01:32 PM|LINK
It *feels* like it is trying to use my media type formatter to handle the conversion of the route parameters...but that is just guessing
SiggiGG
Member
265 Points
105 Posts
Re: Binding to route value and CustomMediaTypeFormatter
Feb 20, 2012 01:33 PM|LINK
Try adding your media types to the formatter's SupportedMediaTypes and see if that works :)