I am developing an application which uses webapi controllers. I just need to set some setting for a Specific controller not ALL! I use IControllerConfiguration and in the initialize method set type name handling setting. I expect that this setting just applied to only one contoller with the attribute but it applies to All, ALL other controllers also use this new setting wrongly! I need to apply custom attribute ONLY on the Schedulecontoller is there any solution please thanks.
public class CustomJson245Attribute : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
{
controllerSettings.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling= TypeNameHandling.Auto;
// formatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
}
}
[CustomJson245]
public class ScheduleController : ApiController
{}
I do not need this controller's setting to change! but it changes as ScheduleSetting.
public class SimpleController : ApiController
{}
EDITED I finally found the answer,
public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
{
controllerSettings.Formatters.Clear();
controllerSettings.Formatters.Add(new JsonMediaTypeFormatter() { SerializerSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto} });
}