Base solution for your next web application
Open Closed

Overriding contract resolver in Dynamic Web Api per action #4533


User avatar
0
fgarcia created

I am using tools that require Json to serialize in PascalCase, and I was able to override the contract resolver with the DefaultContractResolver in the WebApiModule's PostInitialize method. This works great, but unfortunately appears to affect the built in users and role manangement that I would like to keep as is. My desired outcome is the ability to override the contract resolver on a per action basis, either replacing the contract resolver for the methods that ASPNET Zero calls, or the ones I made. I'm pretty sure it wouldn't be too hard to do with regular controllers and some attributes, but something in the pipeline isn't working as I intend with the dynamic web api controllers. I took a crack at adding an Action filter attribute to a method in the IMyApplicationService interface, and it seems to execute the action filter, and during OnActionExecuted, I'm replacing the default contract resolver with a camel case one, but the response is still in PascalCase, which the users page doesn't really appreciate. What is the best way to go about achieving this goal?


3 Answer(s)
  • User Avatar
    0
    fgarcia created

    I think I might have fixed this issue by removing the global default contract resolver and applying the action filter to a specific method on the app service interface. Will continue testing and update accordingly

  • User Avatar
    0
    fgarcia created

    Yeah workaround ended up working. Applied the below linked stack overflow answer, but in reverse (using default contract resolver). Added the DefaultCasingFilter action attribute to the methods in the application service interface, seems to work as intended. Something else was going on with the users and roles that the camel casing wasn't working, but the reverse works just fine.

    <a class="postlink" href="https://stackoverflow.com/a/14532163/1299226">https://stackoverflow.com/a/14532163/1299226</a>

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks a lot @fgarcia for sharing your solution :)