I'm working on a project that is an Asp.net core web API project.most of my projects I did by Asp.net MVC and Core MVC, but I would do a new project based on Core Web API. in Core MVC we have Areas concept that we can create an Areas for Admin part, etc.... But I want to know Can we use Areas in Asp.net core Web API Projects? If yes how can I do it? If you're thinking about there is a better way tell me, please.
You can place your API controllers in an Area, just to group them. But basically you provide the route on the controller, so you can create different routes your self:
[Route("api/myarea/v1/[controller]")]
I have same problem I tried by putting my ApiController under my area so the structure look like below
Here is the Api controller inside
Areas/GL/Controllers
[Area("GL")]
[Route("api/[controller]")]
[ApiController]
[HttpGet]
public IActionResult Get()
{
return Ok("api controller");
}
But when I access my URL
http://localhost:48357/GL/Api/Get
Or
http://localhost:48357/Api/GL/Get
or
http://localhost:48357/Api/Get
I get
Status Code: 404; Not Found
Here is the code in
Startup.cs
routes.MapAreaRoute(
name: "AreaGL",
areaName: "GL",
template: "GL/{controller=Home}/{action=Index}/{id?}"
);
Please help me on this issue, what I am doing wrong here?
Thanks for contacting us, @farhadibehnam.
For the controllers to be in areas you have to apply {area:exists}
route constraint in your attribute route, as described https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/areas?view=aspnetcore-2.2.
Most helpful comment
You can place your API controllers in an Area, just to group them. But basically you provide the route on the controller, so you can create different routes your self:
[Route("api/myarea/v1/[controller]")]