Saturday, October 10, 2020

Create asp.net core web api from scratch

Create ASP.NET Core REST API Project

Use the API project template to create a REST API Project. In our case we want to add the REST API project to the BlazorTutorial solution. Here are the steps.

  1. Right click on the solution in the Solution Explorer and select Add - New Project.
  2. In the Add a new project window, select ASP.NET Core Web Application and click Next
  3. Name the project EmployeeManagement.Api and then click Create

create asp.net core web api from scratch

4. On the next screen, select API project template and click Create

EmployeeManagement.Models.Employee class changes

In the Employees database table, we will be storing DepartmentId. So to keep the Employee model class simple, remove Department property and include DepartmentId property. The Employee class must be as shown now.

public class Employee
{
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public DateTime DateOfBrith { get; set; }
    public Gender Gender { get; set; }
    public int DepartmentId { get; set; }
    public string PhotoPath { get; set; }
}

Add reference to EmployeeManagement.Models project

We need the model classes (i.e Employee and Department) defined in EmployeeManagement.Models project in EmployeeManagement.Api project . So add a project reference.

add reference to EmployeeManagement.Models project

No comments:

Post a Comment

Binding select element with database data in Blazor

  When the   Edit Employee   form loads, we want to retrieve the list of all departments from the database and bind them to the   Department...