Justin Hoffman

  Home  |   Contact  |   Syndication    |   Login
  9 Posts | 0 Stories | 26 Comments | 0 Trackbacks

News

ASP.net Forms Justin Hoffman

Archives

During the upgrade of a solution from the Web Api Preview 6 to MVC 4 Beta Web Api, I encountered a bug with a custom MediaTypeFormatter.  In the Api controller I had a need to take the posted content object as well as some string identifiers/keys as parameters.  The problem I encountered as that if I utilized a custom MediaTypeFormatter, these values came in as Null

Take the following Api controller Post:

       // POST /api/values
        public HttpResponseMessage<SomeObj> Post(string key, string id, SomeObject value)

The string key always came in null.  However, if I switch and used one of the built in formatters XmlMediaTypeFormatter for example, this was not a problem.  It is my understanding that this issue will be fixed post Beta.  I posted my problem on MSDN and did receive a response, you can find it here.

In the meantime, something like the following can be used for a workaround.

Add an Action Filter to handle the parameter binding for you.  Below is an example of such a filter.  It currently would only work with string or int route parameterrs (but can be changed/expanded).

    public class ParameterFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
          if (actionContext.Request.Method != HttpMethod.Get)
            {
                var idParams = actionContext.ControllerContext.Request.GetRouteData();
                foreach (var item in idParams.Values)
                {
                    object value;
                    var result = actionContext.ActionArguments.TryGetValue(item.Key, out value);
                    if (result)
                    {
                        var parm = actionContext.ActionDescriptor.GetParameters().Where(x => x.ParameterName == item.Key).SingleOrDefault();
                        var type = parm.ParameterType;
                        // Check Action parameter type and convert if needed
                        if (type == typeof(int))
                        {
                             actionContext.ActionArguments[item.Key] = Convert.ToInt32(item.Value);
                        }

                        if (type == typeof(string))
                        {
                             actionContext.ActionArguments[item.Key] = item.Value.ToString();
                        }
                    }
                }
            }

            base.OnActionExecuting(actionContext);
            }
    }

Register the Action Filter in the Global.asax

        protected void Application_Start()
        {
            GlobalConfiguration.Configuration.Formatters.Clear();
           
            GlobalConfiguration.Configuration.Filters.Add(new ParameterFilter());
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            BundleTable.Bundles.RegisterTemplateBundles();
        }
posted on Tuesday, March 6, 2012 9:08 AM

Feedback

# re: MVC 4 Beta Web Api: Bug with Binding Route Values when using a custom MediaTypeFormatter 3/9/2012 1:18 AM Jeff Doolittle
Just what I was looking for, thanks!

# re: MVC 4 Beta Web Api: Bug with Binding Route Values when using a custom MediaTypeFormatter 5/15/2012 6:55 AM LittleColin
Excellent - just what I needed.

# re: MVC 4 Beta Web Api: Bug with Binding Route Values when using a custom MediaTypeFormatter 5/21/2012 7:28 PM george
no suitable method found to override? What am I doing wrong?

# re: MVC 4 Beta Web Api: Bug with Binding Route Values when using a custom MediaTypeFormatter 5/22/2012 8:36 AM Justin
@george This is runtime error or you can't get it to compile?

# re: MVC 4 Beta Web Api: Bug with Binding Route Values when using a custom MediaTypeFormatter 5/23/2012 11:45 PM Chintan
actionContext.ControllerContext.Request.GetRouteData();

GetRouteData() not found.
What else we can use?

# re: MVC 4 Beta Web Api: Bug with Binding Route Values when using a custom MediaTypeFormatter 5/24/2012 7:21 AM Justin
@Chintan, make sure you are have correct namespace
using System.Web.Http;
GetRouteData is an extension method in System.Web.Http

# re: MVC 4 Beta Web Api: Bug with Binding Route Values when using a custom MediaTypeFormatter 5/31/2012 2:00 PM Sandra
Can you tell me if you replaced the custom MediaTypeFormatter with the Filter?
I am trying to do a similar action, but in my case, the route values are filled and is the third object that is null. When debugging the formatter the returned object is ok.

# re: MVC 4 Beta Web Api: Bug with Binding Route Values when using a custom MediaTypeFormatter 5/31/2012 2:11 PM Justin
@Sandra.. I didn't replace the MediaTypeFormatter with the filter, I used them both in combindation. In your MediaTypeFormatter in the "CanReadType" method, are you exluding anything? I remember being able to produce your problem by just returning True in CanReadType. But if you exelcude IKeyValueModel it should work

# re: MVC 4 Beta Web Api: Bug with Binding Route Values when using a custom MediaTypeFormatter 5/31/2012 2:25 PM Sandra
Thanks a lot! With that change in the CanReadType I now have the same problem you had :)
Now adding the filter...

Post A Comment
Title:
Name:
Email:
Comment:
Verification: