3

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} });
}
user9137963
  • 105
  • 9
  • Override the constructor in the subclass. – user202729 Jan 07 '18 at 10:45
  • how to do that?, please I am some how new in webapi , thank you very much. what should I do in the overrided constructor? – user9137963 Jan 07 '18 at 10:51
  • This is not related to webapi, just need C# knowledge, which can be searched on the web relatively easily. – user202729 Jan 07 '18 at 10:58
  • thanks a lot my friend , but i didnt get my answer. I can override the constructor but what should I do to specify the setting just to the contoller, not to ALL contollers. this is my question! but again thanks for the response – user9137963 Jan 07 '18 at 11:03
  • Show your effort? Also show the code of one more controller which you don't want to change the setting. – user202729 Jan 07 '18 at 11:27
  • You should answer your own question with your edit above. I spent way too long trying to debug my controllers, turned out to be the same issue. – notracs Nov 26 '18 at 13:15

0 Answers0