Derik Whittaker

Syndication

News


Asp.Net Web Api MediaTypeFormatter Error for x-www-formurlencoded data

As I was breaking out some of my MVC controllers to use Web Api I ran into a not so fun error (seen below) when doing a jQuery Post.

image

I was trying to post a model via jQuery to my Web Api Endpoint and it was failing and it made no sense because when I used Postman via Chrome it would work just fine.  My jQuery code is below

var model = {
    GroupId: self.SelectedGroup().GroupId,
    Name: self.SelectedGroup().Name,
};

$.ajax({
    url: route,
    type: 'Post',
    dataType: "json",
    data: model,
    success: function(data){ 
        alert("passed");
    },
    error: function(data) {
        alert('There was a (' + data.status + ') returned due to ' + data.statusText + '.'); //or whatever
    }
});

My Postman setup looked like

image

Turns out to find my issue i needed to inspect the error result passed back to jQuery which pointed me towards the real error of

"No MediaTypeFormatter is available to read an object of type 'GroupSelectionModel' from content with media type 'application/x-www-form-urlencoded'."

Now that I knew that jQuery was trying to do a FormUrlEncoded post, not a pure json post I had an idea what the root cause was… ME.  I was trying to be clever and only allow for the JsonMediaTypeFormatter and removing ALL other formatters, see below.

image

What I needed to do was the following

image

Once I added the correct formatter it all just WORKED.

Till next time,


Posted 12-20-2012 7:06 AM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Pharmd364 wrote re: Asp.Net Web Api MediaTypeFormatter Error for x-www-formurlencoded data
on 01-25-2013 3:00 PM

Hello! gbekfde interesting gbekfde site! I'm really like it! Very, very gbekfde good!

buy discount viagra wrote re: Asp.Net Web Api MediaTypeFormatter Error for x-www-formurlencoded data
on 02-02-2013 10:55 AM

WlB5oI Very neat post.Thanks Again. Great.

buy discount viagra wrote re: Asp.Net Web Api MediaTypeFormatter Error for x-www-formurlencoded data
on 02-02-2013 5:13 PM

vBjMPP I cannot thank you enough for the blog post.Really thank you!

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)