The main objectives of this article are:
In simple terminology API(Application Programming Interface) means an interface module that contains a programming function that can be requested via HTTP calls to save or fetch the data from their respective clients.
- .NET7 Web API
- Install .NET7 SDK And IDE
- Create A .NET7 Web API Application
- SQL Database ConnectionString
- Entity Framework Core
- Install Entity Framework Core NuGet Packages
- Entity Framework Core DatabaeContext
.NET7 Web API:
Web API is framework for building an HTTP service that can be accessed by any clients like browser, mobile devices, and desktop apps.In simple terminology API(Application Programming Interface) means an interface module that contains a programming function that can be requested via HTTP calls to save or fetch the data from their respective clients.
Some of the key characteristics of API are:
- Supports HTTP verbs like 'GET', 'POST', 'PUT', 'DELETE', etc.
- Supports default responses like 'XML', and 'JSON'. Also can define custom responses.
- Supports self-hosting or individual hosting, so that all different kinds of apps can consume it.
- Authentication and Authorization are easy to implement
Install .NET7 SDK And IDE:
Note: Install .NET7 SDK for .NET CLI users. For Visual Studio users, no need to install it explicitly, they just need to have Visual Studio 2022(Only the latest version supports .NET7). To download the Visual Studio 2022 community version(free version) go to 'https://visualstudio.microsoft.com/downloads/'
Dotnet CLI users must install the .NET7 SDK, so go to the "https://dotnet.microsoft.com/en-us/download/dotnet/7.0" and then download SDK with respect to your machine operating system.
CLI user's best recommended IDE is Visual Studio Code, so go to 'https://code.visualstudio.com/' and then download the IDE.
So to develop dotnet code with VS Code we need to have "C# Visual Studio Code Extension".
Create A .NET7 Web API Application:
Let's create a .NET7 Web API sample application to accomplish our demo. We can use either Visual Studio 2022 or Visual Studio Code(using .NET CLI commands) to create any .NET7 application. For this demo, I'm using the 'Visual Studio Code' editor.
dotnet new webapi -o name_of_your_project
In our API project 'Program.cs' file is the entry file. So let's explore the default services and middleware configured in 'Program.cs' file.
Services In Program.cs:
builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen();
- The 'AddControllers' service for API controllers.
- The 'AddEndpointAPIExplorer' service gives support for minimal API.
- The 'AddSwagerGen' service for the swagger page.
Middlewares In Program.cs:
if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers();
- (Lines: 3&4) The swagger middleware to load the swagger page.
- (Line: 6) The 'UseHttpsRedirect' middleware redirects the 'HTTP' request to 'HTTPs'.
- (Line: 7) The user authorization middleware.
- (Line: 8) The 'MapControllers' is an endpoint middleware that can point to the route to the API controller.
SQL Database ConnectionString:
Let's prepare the SQL connection string.
Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;
- Data Source - SQL server name
- Initial Catalog - Database name
- Integrated Security - windows authentication.
- Connect Time - connection time out period.
Entity Framework Core:
Entity Framework Core is an Object/Relational Mapping(ORM) framework. EF Core makes database communication more fluent and easy. The 'DatabaseContext' class act as a database from our c# code, it will contain all registered classes 'DbSet<TEntity>'(TEntity is any POCO class that represents the table).
Install Entity Framework Core NuGet Packages:
Let's install the entity framework core package.
Package Manager Command
NuGet\Install-Package Microsoft.EntityFrameworkCore -Version 7.0.2
NuGet\Install-Package Microsoft.EntityFrameworkCore -Version 7.0.2
CLI Command
dotnet add package Microsoft.EntityFrameworkCore --version 7.0.2
dotnet add package Microsoft.EntityFrameworkCore --version 7.0.2
Let's install the entity framework core SQL package.
Package Manager Command
NuGet\Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 7.0.2
NuGet\Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 7.0.2
CLI Command
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 7.0.2
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 7.0.2
Setup Entity Framework Core DatabaseContext:
Now add a new class that represents our 'Beach' table. So let's create a new folder like 'Data/Entities' and then add our new class like 'Beach.cs'.
Data/Entities/Beach.cs:
namespace Dot7.API.CRUD.Data.Entities; public class Beach { public int Id{get;set;} public string? BeachName{get;set;} public string? Place{get;set;} public string? ImageUrl{get;set;} }
- Here few properties type post-fixed with '?' means those are nullable properties and represent nullable columns in the table
To manage or control all the table classes in c# we have to create DatabaseContext class. So let's create our context class like 'MyWorldDbContext.cs' in the 'Data' folder.
Data/MyWorldDbContext.cs:
using Dot7.API.CRUD.Data.Entities; using Microsoft.EntityFrameworkCore; namespace Dot7.API.CRUD.Data; public class MyWorldDbContext: DbContext { public MyWorldDbContext(DbContextOptions<MyWorldDbContext> options):base(options) { } public DbSet<Beach> Beach {get;set;} }
- (Line: 6) The 'Microsoft.EntityFramework.DbContext' needs to be inherited by our 'MyWorldDbContext' to act as a database context class.
- (Line: 8) The 'Microsoft.EntityFrameworkCore.DbContextOptions' is an instance of options that we are going to register in 'Program.cs' like 'ConnectionString', 'DatabaseProvider', etc.
- (Line: 13) All our table classes must be registered inside of our database context class with 'DbSet<T>' so that the entity framework core can communicate with the table of the database.
appsettings.Development.json:
"ConnectionStrings": { "MyWorldDbContext":"" }.Register the database context service in the 'Program.cs' file for dependency injection.
Program.cs:
builder.Services.AddDbContext<MyWorldDbContext>(options => { options.UseSqlServer(builder.Configuration.GetConnectionString("MyWorldDbConnection")); });
- Here we have to pass our connection string to the 'UseSqlServer()' service.
Next article we are going to implement the Read Operation.
Support Me!
Buy Me A Coffee
PayPal Me
Video Session:
Wrapping Up:
Hopefully, I think this article delivered some useful information on the .NET 7 Web API and Vue(3.0) application. using I love to have your feedback, suggestions, and better techniques in the comment section below.
Refer:
Part-4 | .NET7 Web API | SQL Database | VueJS(3.0) - Composition API | CRUD Example
Part-5 | .NET7 Web API | SQL Database | VueJS(3.0) - Composition API | CRUD Example
Part-5 | .NET7 Web API | SQL Database | VueJS(3.0) - Composition API | CRUD Example
Comments
Post a Comment