Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I’m going crazy here… this seems like a very simple task. First off, I know only the basics of the Web Api and MVC – so please don’t skewer me.

In the project I need to logically create controller subfolders (for organization purposes). I had a feeling it wasn’t as simple as I thought. I have the default route like this:

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

Which works as it should directly from the controllers folder in my project. I have added a subfolder in the controllers folder controllers/reports. I have searched quite a bit and just can't quite find a solution. How can I add a route that will direct to the subfolder. I have tried:

 config.Routes.MapHttpRoute(
            name: "ReportingApi",
            routeTemplate: "api/Reports/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }

and:

 config.Routes.MapHttpRoute(
            name: "ReportingApi",
            routeTemplate: "api/Reports/{id}",
            defaults: new { controller = "userunit" id = RouteParameter.Optional }
share|improve this question
    
Routing doesn't actually use the filesystem, it routes to controller actions. – Mister Epic Oct 9 '13 at 20:36
    
But don't you at some point have to provide a path if a controller is in a subfolder? It doesn't just find it does it? – Mike Schwartz Oct 9 '13 at 20:37
    
By convention, the framework looks for controllers in the Controller folder, but beyond that it's not looking at paths. Why do you need to create a subfolder for every controller? If you want, you can fiddle with routing to get the URLs just right, if that is what you are after. – Mister Epic Oct 9 '13 at 20:41

1 Answer 1

up vote 3 down vote accepted

Nevermind I'm an idiot... I left the default route in, removed the "Reports" in the url. It found the controller even though it was in a subfolder.

share|improve this answer
    
I've got a feeling this is gonna cause trouble down the line. What if you've got two identically named controllers separated by different namespaces due to the use of subfolders. This is simply an oversight in the WebApi design. – MoonStom Oct 5 at 18:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.