Customizable Razor templates for ASP.NET Core Web API controllers
Install package and add ASP.NET Web API Templates:
Use the template to generate Web API controllers:
Run and/or re-create the sample:
To install the templates package open a terminal and run:
For the latest stable version:
dotnet new -i "AspNetCore.WebApi.Templates::*"
To uninstall the templates package open a terminal and run:
dotnet -u "AspNetCore.WebApi.Templates"
To install templates in an ASP.NET Core Web API project, open a terminal at the project root and run:
dotnet new webapi-templates
A Templates folder will be added containing a ControllerGenerator folder with Razor templates that can be customized to generate Web API controllers.
Important: You must explicitly set the Build Action of each .cshtml file to
None
in order to build the project.
You can add Web API controllers using these templates either from Visual Studio or the terminal.
Follow these step to re-create the sample from scratch.
Open SQL Server Management Studio and create a new database named NorthwindSlim.
Create a new ASP.NET Core Web API project.
Add the following NuGet package:
Microsoft.EntityFrameworkCore.SqlServer
Reverse engineer context and entity classes from the NorthwindSlim database.
Note: This sample uses a database-first approach, but you can also use a code-first approach by using EF Core code migrations.
dotnet ef dbcontext scaffold "Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=NorthwindSlim; Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -o Models -c NorthwindSlimContext -f
Scaffold-DbContext "Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=NorthwindSlim; Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context NorthwindSlimContext -Force
OnConfiguring
method from the generated NorthwindSlimContext
class.Configure the web app to use the NorthwindSlim database.
"ConnectionStrings": {
"NorthwindSlimContext": "Data Source=(localdb)\\MSSQLLocalDB;initial catalog=NorthwindSlim;Integrated Security=True; MultipleActiveResultSets=True"
}
ConfigureServices
method in Startup.cs to accommodate cyclical references and register the DbContext class.public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddJsonOptions(options =>
options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.All);
var connectionString = Configuration.GetConnectionString(nameof(NorthwindSlimContext));
services.AddDbContext<NorthwindSlimContext>(options => options.UseSqlServer(connectionString));
}
If you execute dotnet new
from a terminal at the project root, you'll see a list of available templates, including ASP.NET Core Web API Templates, which you installed earlier.
dotnet new webapi-templates
None
.Add a Values controler with read/write actions using Visual Studio.
GET: http://localhost:53225/api/values
GET: http://localhost:53225/api/values/5
POST: http://localhost:53225/api/values
To add a controller from a terminal without using Visual Studio you can execute the dotnet aspnet-codegenerator controller
command.
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
</ItemGroup>
dotnet restore
from the command line.dotnet aspnet-codegenerator --help
for a list of options.-f
overwrites previous file):dotnet aspnet-codegenerator controller -name Values -api -actions -outDir Controllers -f
Add a Products API controller with actions using Entity Framework.
You can use Visual Studio to add the controller, selecting Product for the model class and NorthwindSlimContext for the data context class.
To generate the Products controller from a terminal run:
dotnet aspnet-codegenerator controller -name ProductsController -api -m Product -dc NorthwindSlimContext -outDir Controllers -f
Use Postman to issue requests to the Products controller.
GET: http://localhost:53225/api/products
GET: http://localhost:53225/api/products/1
POST: http://localhost:53225/api/products
{
"productName": "Chocolato",
"categoryId": 1,
"unitPrice": 23,
"discontinued": false
}
{
"$id": "156",
"productId": 1078,
"productName": "Chocolato",
"categoryId": 1,
"unitPrice": 23,
"discontinued": false,
"rowVersion": "AAAAAAAAF3E=",
"category": null,
"orderDetail": {
"$id": "157",
"$values": []
}
}
PUT: http://localhost:53225/api/products/1078
{
"productId": 1078,
"productName": "Chocolato - Updated",
"rowVersion": "AAAAAAAAF3E="
}
DELETE: http://localhost:53225/api/products/1078
GET: http://localhost:53225/api/products/1078