Name Collisions with Asp.Net Webapi and How to Avoid

I love the simplicity of using ASP.NET WebAPI for building simple web rest services.  No special calls to create JSON, simple REST conventions implemented with the methods GET,POST,PUT and DELETE and very straight forward interfaces.

The thing that annoys me is the naming convention.  In my case, I’ve create a folder /rest on my web server and I put all my WebAPI controllers there.  An example of a controller looks as follows:

 

The problems comes up when you decide you want to have a different FAQ controller for different parts of the web site.  The obvious solution is to drop the REST services into sub directories.  Unfortunately, since the class name above does not change, you have conflict.  I’m guessing there is some attribute to solve this but it just feels clunky to me.

About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP since 2007. In addition, he's a multi-course video author at Pluralsight. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


Comments

  1. Peter Kellner says:

    I think that is the problem. Routing is folder based on webapi alls controllers based on their name. It really does not matter what the route is. I think it will always find the webapi controller based on it’s class name and not the route it lives in.

  2. Solution man says:

    Yes, that is a nice naming convention.

    I guess, the real solution must be near to the routing.
    Normally: /api/controllername
    Routing trick: /api/modulname/controllername
    In this case I think all the controllers must be in a modulname folder.

  3. Peter Kellner says:

    Good point. I guess I should have said that. Basically, unless someone can tell me otherwise, don’t nest your directories in the /rest folder. I’ve started doing filenames as my nesting:

    rest/AdminPeopleController
    rest/AdminDogController

    etc

  4. Solution Man says:

    so, what is the solution?

Trackbacks

  1. […] Name Collisions with Asp.Net Webapi and How to Avoid (Peter Kellner) […]

Follow

Get every new post delivered to your Inbox

Join other followers: