.NET 5.0 officially released on November 10, 2020. As per Microsoft, .NET 5 is the next step of the .NET Core. Also .NET Framework and .NET Core will become a single framework in the next .NET 6 release. Its always good to be prepared, So today we are going to try to build a .NET 5 Web API. For this we are building a simple Web API to manage a movies database. This will include the basic CRUD operations. We start with the beginner level.
First we need to update Visual Studio to support the .NET 5.0 if you haven’t updated yet. I’m using VS Community 2019 Preview, Version 16.9.0 Preview 1.0 for this article.
Create a new project
Lets create a new project in Visual Studio. Select ASP.NET Core Web Application then give a name MovieWebAPI as the Project name. Then select the template ASP.NET Core Web API. For now we select No Authentication.
Once the project created, just run the project. We can see the default API method comes with the template.
This nice UI is from Open API specification(Swagger) which comes by default for .NET core API projects. Later we will discuss how to modify this API specification according to our requirements. Following image shows how the swagger defined in Startup.cs file.
Install nuget packages
Now lets install some required nuget packages. So install all of the following packages if not installed yet. Some of them will come by default with .NET 5 Web API template.
We dont need the default API method anymore, So delete the WeatherForecast.cs class and WeatherForecastController inside Controllers folder.
Adding Classes and Controller
Lets add our model class Movie. Add a class inside Models(Create a new folder) folder.
public class Movie
{
public int Id { get; set; }
[Required]
[StringLength(60, MinimumLength = 3)]
public string Title { get; set; }
[Required]
[StringLength(30, MinimumLength = 1)]
[RegularExpression(@"^[A-Z]+[a-zA-Z""'\s-]*$")]
public string Genre { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
}
Now we need to create the DB and table to store Movies. Inside Data folder(Create new folder), Add the DB context class.
public class MovieAPIContext : DbContext
{
public MovieAPIContext(DbContextOptions<MovieAPIContext> options)
: base(options)
{
}
public DbSet<Movie> Movie { get; set; }
}
After that add the DB connection string in the appsettings.json file and then run the pacckage manager console command PM> Add-Migration Initial. This will create initial migration. Once you run the PM> Update-Database it will create the DB in DB server.
"ConnectionStrings": {
"MovieAPIContext": "Server=YourServer;Database=MovieDatabase;Trusted_Connection=True;MultipleActiveResultSets=true"
},
As next step we need to add our API controller. Add a new controller inside the controllers folder. For this add MVC Controller-Empty and select API COntroller-Empty. Then name the controller MoviesController. Next we add the constructor and our first Get method to fetch the movies from database.
private readonly MovieAPIContext _context;
public MoviesController(MovieAPIContext context)
{
_context = context;
}
// GET: api/Movies
[HttpGet]
public async Task<ActionResult<IEnumerable<Movie>>> GetMovie()
{
return await _context.Movie.ToListAsync();
}
Done! Now we can run the project and see our newly added method.
We can insert some sample movies data into our database and execute the Get method.
Also we can test our first method with Postman tool as well. So far we have a one working method with our Movie API. Lets discuss how to do the Post and Put methods in next article. Enjoy coding!
1 thought on “.NET 5 Web API Introduction”