Base solution for your next web application
Open Closed

How to apply Formatters.JsonFormatter for WebApi #1462


User avatar
0
princedis created

In Normal WebAPi project we do Formatters.JsonFormatter.SerializerSettings inside public static void Register(HttpConfiguration config). But in AspNetZero Dynamic WebApi how do I do formating.

Basically I am looking to do Newtonsoft.Json.Converters.StringEnumConverter.


1 Answer(s)
  • User Avatar
    0
    princedis created

    Inside of WebApi Project under override Initialize

    public override void Initialize()
            {
                IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
    
                //Automatically creates Web API controllers for all application services of the application
                DynamicApiControllerBuilder
                    .ForAll<IApplicationService>(typeof(ZeroApplicationModule).Assembly, "app")
                    .Build();
    
                Configuration.Modules.AbpWebApi().HttpConfiguration.Filters.Add(new HostAuthenticationFilter("Bearer"));
    
                //convert Enums to Strings (instead of Integer) globally
                Newtonsoft.Json.JsonConvert.DefaultSettings = (() =>
                {
                    var settings = new Newtonsoft.Json.JsonSerializerSettings();
                    settings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter { CamelCaseText = true });
                    return settings;
                });
    
                ConfigureSwaggerUi(); //Remove this line to disable swagger UI.
            }