Search code examples
c#asp.netjsonumbraco

Return json as default from Umbraco API Controller


As the title says I would like to return json as default instead of XML. In a normal Web API i can edit App_Start/WebApiConfig.cs and add the following line but I can't find where to edit the configuration in Umbraco.

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

I do not wan't my method to return JsonResult to achieve this.


Solution

  • I looked at a similar solution first @sebastiaan but decided to override "ApplicationStarting" instead.

    public class CustomApplicationEventHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }
    }
    

    And the WebApiConfig class:

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        }
    }