DEV Community

Cover image for Adding Web Api Controller to a Razor Page ASP.NET Core app
jicking bebiro
jicking bebiro

Posted on

Adding Web Api Controller to a Razor Page ASP.NET Core app

This is part of a personal ASP.NET Core cookbook that i am working on.

Open an existing asp.net core 5 razor pages app.

Add Swashbuckle.AspNetCore via nugget. This will auto generate api docs, cool right?
image

Update Startup.cs to include controller and swagger services and middlewares.
image

At project root, add Controllers directory then open context menu under that folder by selecting the folder and right click.
On context menu, click add > controller

A new dialog window will open for scaffolding options.
Select API.
image

Run project then go to https://localhost:PORT/swagger
image

There you have it, happy hacking.

Top comments (0)