Aspnetcore: Can I use Areas in Asp.net core web API projects?

Created on 19 Jan 2019  ·  3Comments  ·  Source: dotnet/aspnetcore

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.

area-mvc

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]")]

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

guardrex picture guardrex  ·  3Comments

markrendle picture markrendle  ·  3Comments

dotNETSanta picture dotNETSanta  ·  3Comments

FourLeafClover picture FourLeafClover  ·  3Comments

rynowak picture rynowak  ·  3Comments