11

Is there a way to add sub folders in the Controller folder of a Webapi project?

I'm thinking of something like Controller/Customer includes the controllers for the Customer module.

1
  • what I want is to have the sub folder name in the URL. i.e: lolcalhost/weApp/{foldername}/{controller} Commented Dec 4, 2012 at 12:14

4 Answers 4

6

Controllers don't work in that way. In Asp.NET your folder structure is your website structure. In WEB API, controllers are looked for independently of the structure. As a matter of fact, you can have them in different assembly and they still will be found by the framework. The routing will not work the way you expect. You can add a route where you have your "folder name", like you said, localhost/WebApp/{foldername}/{controller}. Only {foldername} can be plain, static folder name (localhost/WebApp/foldername/{controller}). So the client will have to call url with "foldername" in it but the location of code wouldn't matter because MVC framework doesn't differentiate folder trees under controllers.

Sign up to request clarification or add additional context in comments.

Comments

3

The answers here are wrong. You can easily do this. You just need to then specify the route on your controller class:

Located in /Controllers/Authentication folder

//An example of you specifying a diff. route than the folder path
[Route("api/login")]
public class LoginController {...}

1 Comment

I think in your example here, it should be [RoutePrefix("api/login")] and not [Route(...)]. The [Route(...)] attribute would go onto individual actions inside the LoginController class.
2

I used areas to solve this problem. As specified here http://blogs.infosupport.com/asp-net-mvc-4-rc-getting-webapi-and-areas-to-play-nicely/

Comments

2

What I do to solve this issue for my self is adding another Maproute and add namespace before the controller. Just be aware of two points:

  1. Your new rout should be before the Default one
  2. You cannot add the namespace for the default one

        routes.MapRoute(
           name: "Secretariat",
           url: "{namespace}/{controller}/{action}/{id}",
           defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
       );
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.