ASP.NET Core MVC with /api/-area

Having MVC pages and API in the same solution can be beneficial. Projects may share a lot of boilerplate code and settings, is easier to maintain and can access API functions directly through DI (if controllers are registered there) instead of going via HTTP. But you may want to separate both URL and code so it is clear what is MVC and what is API.

Setting up areas

If you are using Visual Studio: Right-click project, select Add, select Area, enter “api”. This will create empty folder Areas\api with MVC folder structure under it. It also pops up a text file asking you to make a change to the default MapRoute in Startup.cs.

If using Rider or other tool you may have to do it manually. Create folders in root:

  • Areas\api\Controllers
  • Areas\api\Models

Then in addition to your existing default route, you should add areas route in Startup.cs.

Adding Api controller

Again, if you are using Visual Studio, simply:

  • Right click “Areas\Api\Controllers”
  • Select “Add->Controller”
  • Select “API Controller – Empty” and name it, for this test I named it TestController

If you are using Rider or other tool you may have either create manually (see bellow for sample) or create Controller and modify it a bit. Change “Controller” to “ControllerBase”, add [ApiController] tag and set route.

In the newly created TestController.cs we need to specify route, specifically [action]. This can be done in class route definition, or per method. With a few example methods the class looks like this:

Test

You can then test this by starting solution and opening browser to https://localhost:44378/Api/Test/Concat/123/abc. Note that you must change port number to whatever port your solution is running on.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Tedds blog

Subscribe now to keep reading and get access to the full archive.

Continue reading