Web API routing doesn't appreciate non-public controllers RRS feed

  • Question

  • User354872849 posted


    Create default netcoreapp2.2 web api project (I use Rider, but I believe it doesn't really matter).

    Start the project.

    Call an action in a controller and ensure that it works.


    Change the controller's access modifier from public to internal

    Expected behavior

    The same api call works

    Actual behavior

    The same api call return 404

    Why I care

    I used to close access modifiers as hard as I can. Once I saw developers that references anything from anywhere for no reason, and it was really hard to track. I'd love to have a flow that could warn me about wrong references.

    If a class should not be referenced from another project, it's access modifier should be internal. Well, due to that's what internal means.

    The actual flow seems weird to me: reflection should not think about access modifiers. For example, you are able to (de)serialize any class with any fields.

    Unfortunately, I couldn't find a code where the route resolving happens. Could anyone link the file?

    Friday, August 9, 2019 8:07 AM

All replies

  • User753101303 posted


    Try arround https://stackoverflow.com/questions/43739794/asp-net-core-overriding-the-default-controllerfactory as well as  https://github.com/aspnet/AspNetCore/tree/master/src and should find the relevant place and how to plug your own logic.

    As any design decision it could be discussed.  They likely considered the end result ie technically speaking your code IS called from outside of the assembly. Note also that if your controller is using an internal method it could be then exposed unexpectedly.

    Here it seems it would be best to discusss with the developer about why it is bad and how it could be done otherwise rather than hoping this technical change would improve his programming skills.

    I would definitively keep the default convention... Depending on your VS edition you have tools what could help (you are using the Community Edition ?)

    Friday, August 9, 2019 8:27 AM