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?
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 {
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.
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);
}
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
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?
Hi Scott,
Did you work out how to do this? I have the same issue.
cheers,
mark
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):
Hope this helps,
Benjamin
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.
This is not an ideal solution, however I am returning the JsonResult using serializerSettings (which can be configured globally).
Ex:
is working on a reply...