0

I want to write a collection of Single Page Apps in .NET using MVC and Angular JS (1.x). I would like to manage the bootstrap of each SPA using MVC all within a single project so that the look and feel is consistent using the _Layout and Return view(); But I would like the data binding in the ng $scope to get and post via api calls. How do you mix MVC controllers and API controllers in the same project when they could logically have the same name. E.g. The MVC controller to bootstrap the Product SPA would be ProductController : Controller and the data calls would be in an API controller ProductController : apiController

The routes to these would be different: baseURL/products/... for the MVC and baseURL/api/products/... for the API calls. How does the router know which controller to call as they do not technically have to live inside the Controllers folder. Prefixing the API Controllers with api seems a bit of a cludge and creates ugly URLS - granted they should only ever be consumed by the javascript but if I want to make them available externally I would prefer them to be clean.

Aaron Reese
  • 131
  • 11
  • Use a different namespace? – Obversity Jun 09 '15 at 19:50
  • Isn't this all set up for you when you create a new Web Application and choose MVC and then select Add folders for Web API? – Yuriy Faktorovich Jun 09 '15 at 19:51
  • @Obversity. Thanks for the speeds response; I am still getting to grips with .NET and namespace is not something I have really investigated yet. How does the namespace relate to the routing and how would it determine which controller should be used. How would you recommend setting up the namespaces mvc.product.app and api.product.app or should the mvc/api bit go at the end? from a tortological perspective they should be in the same namespace as they both form part of the same process. – Aaron Reese Jun 09 '15 at 19:55
  • you can configure your controllers through RouteConfig and WebAPIConfig files.It will diffrerentiate your MVC Controller and WebAPI Controller in a project – ReeganLourduraj Jun 09 '15 at 19:57
  • @Yuri. Idon't think so, you still only get one Controllers folder and when you create a Controller class file you have to decide whether to use MVC or API scaffolding so you still end up with two files with the same name and two classes with the same name unless you can find a way to differentiate them. – Aaron Reese Jun 09 '15 at 19:57
  • @ShineKing. Yes and you have default routes set up: /{controller}/{action}/{id} for MVC and /api/{controller}/{action}/{id} for APIconfig, but I still don't get how routes knows which controller to use If I have two controllers with the same name (to have two controllers with the same name I would have to have a Controllers/MVC and Controllers/API subfolders) but seeing as the folder structure is for source and not build, I still don't see how the router can determine the correct controller class to be called. – Aaron Reese Jun 09 '15 at 20:01
  • @AaronReese you could just name it with Api or Service at the end to differentiate the names. But the routes are already set up for you. – Yuriy Faktorovich Jun 09 '15 at 20:03
  • @yuri, yes but then my external url for the api is baseurl/api/apiProduct/.... which is ugly because it has api in it twice. I already know it is an api call because of the first segment of the url; why would I want to have it in the second part too. – Aaron Reese Jun 09 '15 at 20:05
  • @AaronReese modify the route to be /services? Still a little iffy with the /services/ProductApi, but I don't think it's that bad. – Yuriy Faktorovich Jun 09 '15 at 20:07
  • @AaronReese if the path is a big deal, you could always add RouteAttributes to your Actions to be more specific. such as [Route("api/Product/Create")] etc. so your class name can be ProductApi. Or figure out a more global way of handling that? – Yuriy Faktorovich Jun 09 '15 at 20:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80104/discussion-between-aaron-reese-and-yuriy-faktorovich). – Aaron Reese Jun 09 '15 at 20:12

1 Answers1

0

Have a look at here. I think it would be better to use a separate project for WebAPI and MVC.

Community
  • 1
  • 1
Murat Yıldız
  • 11,299
  • 6
  • 63
  • 63