Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Scott Reed 25 posts 69 karma points
    Aug 26, 2014 @ 11:02
    Scott Reed
    0

    WebApiConfig settings when using UmbracoApiController

    Hi guys, I've created many WEB API based projects before and I always the following code in my WebApiConfig Register method

            /// <summary>
            /// Registers the specified configuration.
            /// </summary>
            /// <param name="config">The configuration.</param>
            public static void Register(HttpConfiguration config)
            {
                var formatters = GlobalConfiguration.Configuration.Formatters;
                var jsonFormatter = formatters.JsonFormatter;
                var settings = jsonFormatter.SerializerSettings;
    
                var enumConverter = new Newtonsoft.Json.Converters.StringEnumConverter();
                jsonFormatter.SerializerSettings.Converters.Add(enumConverter);
    
                settings.Formatting = Formatting.Indented;
                settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    
                config.Formatters.Remove(config.Formatters.XmlFormatter);
    
                config.Routes.MapHttpRoute(
                    name: "WebApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new
                    {
                        id = RouteParameter.Optional
                    });
            }

    This removes XML as an output option and sets the JSON response to be properly JSON formatted. These settings seem to make no difference when using the UmbracoApiController. Is there a settings area on the API I can call these?

  • mark firth 29 posts 72 karma points
    Sep 11, 2014 @ 18:20
    mark firth
    0

    Hi Scott,

    Did you work out how to do this? I have the same issue.

    cheers,

     

    mark

     

  • Benjamin Howarth 296 posts 762 karma points c-trib
    Sep 11, 2014 @ 18:47
    Benjamin Howarth
    0

    Hey guys,

    This is how I'm doing it in a current project, using a custom ApplicationEventHandler class (Umbraco automatically discovers these at startup and then runs the appropriate methods that override the base virtual methods):

    public class AppStartupHandler : ApplicationEventHandler {

            protected override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) {
                base.ApplicationInitialized(umbracoApplication, applicationContext);
                var jsonSettings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
                jsonSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All;
            }
    }

    Hope this helps,

    Benjamin

  • Scott Reed 25 posts 69 karma points
    Sep 11, 2014 @ 18:51
    Scott Reed
    0

    The problem I have was the project I was working with upgraded from 6 and still had thes standard asp.net global application event handlers. Once I moved the register call in to the ApplicationStartup inherited from the Umbraco application class it all worked. As a side note be careful if you work with some plugins like Courier because the Angular application classes call web apis and expect upper case, we had to rewrite some angular js files.

  • Kevin Farrugia 22 posts 54 karma points
    Dec 05, 2015 @ 15:17
    Kevin Farrugia
    0

    This is not an ideal solution, however I am returning the JsonResult using serializerSettings (which can be configured globally).

    Ex:

        var jsonSerializerSettings = new JsonSerializerSettings
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver(),
            NullValueHandling = NullValueHandling.Ignore
        };
    
        [HttpGet]
        public JsonResult<object> Get(int? id = null)
        {
            // do your magic
    
            return Json<object>(new
            {
                id = id,
                categories = categories
            }, jsonSerializerSettings);
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft