Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I store WebAPI controllers inside ASP.NET-MVC 5 project?

I have completed an ASP.NET-MVC5 application(website) where I have lot of MVC controllers: enter image description here

I would like to extent the functionality of my application by exposing WEB API with OData.

For instance I would like to create another controller for Person model class, but this time it should be Web API not MVC controller. Should I WEB API store it in Controller folder and just call it PersonWebAPIController? Will it work?

like image 490
Yoda Avatar asked Jun 20 '15 10:06

Yoda


2 Answers

To answer your question Will it work? yes

But if you want to separate code physically, then you can create a custom folder under Controllers folder, and place all the ApiControllers in newly created folder. If you want to separate MVC and API controllers logically, then use different namespaces for them.

Technically you can have controllers in any custom made folder under the Visual studio project.

like image 50
ramiramilu Avatar answered Sep 22 '22 16:09

ramiramilu


You can remove the default routing and use attribute based routing to achieve this. Add all your ApiControllers to an "API" folder with a [Route] attribute with correct routing.

Please have a look at this link for more details.

like image 30
sree Avatar answered Sep 19 '22 16:09

sree