Default formatter

Topics: Web Api
Nov 24, 2011 at 7:41 PM

By default the Web Api (v5) returns application/xml when no accept parameter is set in the request header. How can i change the default to a custom formatter?

I've tried webApiConfig.Formatters.Clear() but that doesn't work.

Nov 24, 2011 at 8:05 PM

After you clear the formatters, did you add the JsonMediaTypeFormatter back?

var formatters = config.Formatters;
formatters.Clear();
formatters.Add(new JsonMediaTypeFormatter());

Thanks,
Maggie Ying

Coordinator
Nov 24, 2011 at 10:13 PM
Try using insert putting json at 0.

Sent from my Windows Phone

From: bertt
Sent: 11/24/2011 11:41 AM
To: Glenn Block
Subject: Default formatter [wcf:280762]

From: bertt

By default the Web Api (v5) returns application/xml when no accept parameter is set in the request header. How can i change the default to a custom formatter?

I've tried webApiConfig.Formatters.Clear() but that doesn't work.

Nov 24, 2011 at 11:45 PM

thanks but it doesn't work... my global.asax now looks like:

var webApiConfig = new WebApiConfiguration(){ EnableTestClient = true };
webApiConfig.Formatters.Clear();
webApiConfig.Formatters.Insert(0, new JsonMediaTypeFormatter());
RouteTable.Routes.MapServiceRoute<RootResource>("", webApiConfig);
 

I'm expecting the Web Api returns now Json by default when hitting the root with no accept parameter in the request header, but i'm always getting xml back...

Nov 26, 2011 at 1:58 PM

well, i´m getting mad, too. i have the same problems. i´m using the assemblies of latest source code...

My custom media type formatter is not triggered at all (even if the request explicitely contains accept: application/hal+xml)

public class Global : HttpApplication
    {
        public const string BaseUri = "http://localhost:2888/";

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            var config = new WebApiConfiguration
            {
                EnableTestClient = true,
                EnableHelpPage = true
            };
            config.Formatters.Insert(0, new HalMediaTypeFormatter(Domain.AdditionalNamespaces));
            
            RouteTable.Routes.SetDefaultHttpConfiguration(config);
            // Extension to map all APIs automatically with
            // the DefaultHttpConfiguration
            RouteTable.Routes.AutoMapServiceRoutes();
        }
    }

Nov 26, 2011 at 6:39 PM
bertt wrote:

thanks but it doesn't work... my global.asax now looks like:

var webApiConfig = new WebApiConfiguration(){ EnableTestClient = true };
webApiConfig.Formatters.Clear();
webApiConfig.Formatters.Insert(0, new JsonMediaTypeFormatter());
RouteTable.Routes.MapServiceRoute<RootResource>("", webApiConfig);

I'm expecting the Web Api returns now Json by default when hitting the root with no accept parameter in the request header, but i'm always getting xml back...

Hi bertt,

Can you show us how your service operation looks like? I am interested in knowing what the return value looks like (if its a regular C# object or a HttpResponseMessage type).

thanks,

Kiran Challa

Nov 26, 2011 at 6:41 PM
humbrie wrote:

well, i´m getting mad, too. i have the same problems. i´m using the assemblies of latest source code...

My custom media type formatter is not triggered at all (even if the request explicitely contains accept: application/hal+xml)

 

public class Global : HttpApplication
    {
        public const string BaseUri = "http://localhost:2888/";

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            var config = new WebApiConfiguration
            {
                EnableTestClient = true,
                EnableHelpPage = true
            };
            config.Formatters.Insert(0, new HalMediaTypeFormatter(Domain.AdditionalNamespaces));
            
            RouteTable.Routes.SetDefaultHttpConfiguration(config);
            // Extension to map all APIs automatically with
            // the DefaultHttpConfiguration
            RouteTable.Routes.AutoMapServiceRoutes();
        }
    }

 

Hi humbrie,

Can you show us how your custom formatter class looks like? One issue that we are aware of is that, when you register a media type in the SupportedMediaTypes collection, do NOT set the 'charset' information...otherwise your formatter wouldn't get picked.

thanks,

Kiran Chala

Nov 26, 2011 at 7:15 PM

RootResource is just very basic:

    [ServiceContract]
    public class RootResource
    {
        [WebGet(UriTemplate = "")]
        public string Get()
        {
            return "test";
        }
    }

 

Nov 27, 2011 at 1:54 AM

you can do the following to make json the default:

JsonMediaTypeFormatter jsonFormatter = config.Formatters.JsonFormatter;
            config.Formatters.Remove(jsonFormatter);
            config.Formatters.Insert(0, jsonFormatter);

Nov 27, 2011 at 10:28 AM

ok thanks that works!