- ASP.NET Core MVC
- ASP.NET Core logging with Serilog
- ASP.NET Core managed config
- ASP.NET Core MVC with /api/-area
- ASP.NET Core API versioning
- ASP.NET Core API with Swagger UI
- ASP.NET Core EF in separate project
- ASP.NET Core AutoMapper
- ASP.NET Core response cache
- ASP.NET Core memory cache
- ASP.NET Core TypeScript
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.
1 2 3 4 5 6 7 8 9 10 |
app.UseMvc(routes => { routes.MapRoute( name: "areas", template: "{area:exists}/{controller=Home}/{action=Index}/{id?}" ); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
[Route("api/[controller]")] [ApiController] public class TestController : ControllerBase { [Route("[action]/{a}/{b}")] [HttpGet] public string Concat(string a, string b) { return a + b; } [Route("[action]")] [HttpGet] public string GetOnly() { return "Can't post here"; } [Route("Dual")] [HttpGet] public string DualGet() { return "You used GET"; } [Route("Dual")] [HttpPost] public string DualPost() { return "You used POST"; } [Route("[action]/{id}")] [HttpGet] public IActionResult ReturnHttpCode(int id) { if (id==0) return NotFound(); return Ok("You found it!"); } } |
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.
