Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I use Areas in Asp.net core web API projects? #6873

Closed
farhadibehnam opened this issue Jan 19, 2019 · 3 comments
Closed

Can I use Areas in Asp.net core web API projects? #6873

farhadibehnam opened this issue Jan 19, 2019 · 3 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Comments

@farhadibehnam
Copy link

farhadibehnam commented Jan 19, 2019

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.

@vankampenp
Copy link

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

@Eilon Eilon added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jan 23, 2019
@atifamin
Copy link

atifamin commented Apr 23, 2019

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?

@mkArtakMSFT
Copy link
Member

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.

@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

5 participants