The post How to Create .Net Core Web API – CRUD Operation appeared first on Tech Insights.
]]>Previous Article Also check Generate Report in PDF Format
Select Frame Work Click on Create
Now Add the Database connection in the appsettigns.json
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "ConnectionStrings": { "sqlconnection": "Server=(local)\\MSSQL;Database=Inventory;Trusted_Connection=True;MultipleActiveResultSets=true" }, "AllowedHosts": "*" }
using System.ComponentModel.DataAnnotations; using System.Text.Json.Serialization; namespace Web_API_Tutorials.Entities { public class Employee { [Key] public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string designation { get; set; } public string Title { get; set; } public string Email { get; set; } [Required] [EnumDataType(typeof(Role))] public string Role { get; set; } [JsonIgnore] public string Password { get; set; } } }
namespace Web_API_Tutorials.Entities { public enum Role { Admin, SuperAdmin, User, } }
Helper Folder Contains DbContext , AppException, Middleware Error Handler
DbContext Class. The DbContext is used for accessing application data through Entity Framework Core and is configured to connect to a SQL Server database. It inherits from the Entity Framework Core DbContext class.
using Microsoft.EntityFrameworkCore; using Web_API_Tutorials.Entities; namespace Web_API_Tutorials.Helpers { public class DbContextData : DbContext { public DbContextData(DbContextOptions<DbContextData> option):base(option) { } public virtual DbSet<Employee> Employee { get; set; } } }
App Exceptions – The app exception is a custom exception class used in the .NET API to differentiate between handled and unhandled exception. Unhandled exceptions are generated by the .NET framework or caused by bugs in application code , where handled exception are generated by application code and used to return friendly error message or user define error, such as business logic or validation exception caused by invalid request parameters
using System.Globalization; namespace Web_API_Tutorials.Helpers { public class AppException :Exception { public AppException() { } public AppException(string message):base(message) { } public AppException(string message,params object[] args) : base(String.Format(CultureInfo.CurrentCulture,message ,args)) { } } }
Error Handler Is used to catch all error and remove the need for duplicated error handling code throughout the .Net 6.o API. It’s configured as middleware in the program.cs file.
using System.Net; using System.Text.Json; namespace Web_API_Tutorials.Helpers { public class MiddlewareErrorHandler { private readonly ILogger _logger; private readonly RequestDelegate _errorHandler; public MiddlewareErrorHandler(ILogger<MiddlewareErrorHandler> logger, RequestDelegate errorHandler) { _logger = logger; _errorHandler = errorHandler; } public async Task Invoke(HttpContext context) { try { await _errorHandler(context); } catch (Exception Err) { var resp = context.Response; resp.ContentType= "application/json"; switch (Err) { case AppException e: resp.StatusCode = (int)HttpStatusCode.BadRequest; break; case KeyNotFoundException e: resp.StatusCode = (int)HttpStatusCode.NotFound; break; default: _logger.LogError(Err, Err.Message); resp.StatusCode = (int)(HttpStatusCode.InternalServerError); break; } var outresult = JsonSerializer.Serialize(new { Message = Err?.Message }); await resp.WriteAsync(outresult); } } } }
using System.Runtime; using AutoMapper; using Web_API_Tutorials.Entities; namespace Web_API_Tutorials.Models { public class AutoMapper :Profile { public AutoMapper() { CreateMap<CreateEmpRequest, Employee>(); CreateMap<UpdateEmpRequest, Employee>(); } } }
The Create Employee Request Model define the parameters for incoming POST requests to the route.
using System.ComponentModel.DataAnnotations; using Web_API_Tutorials.Entities; namespace Web_API_Tutorials.Models { public class CreateEmpRequest { [Required] public string Title { get; set; } [Required] public string FirstName { get; set; } [Required] public string LastName { get; set; } [Required] public string designation { get; set; } public string Email { get; set; } [Required] public Role Role { get; set; } [Required] [MinLength(8)] public string password { get; set; } [Required] public string confirmpassword { get; set; } } }
The Update Employee Request Model define the parameters for incoming PUT requests to the route.
using System.ComponentModel.DataAnnotations; using Web_API_Tutorials.Entities; namespace Web_API_Tutorials.Models { public class UpdateEmpRequest { public string Title { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string designation { get; set; } [EmailAddress] public string Email { get; set; } [EnumDataType(typeof(Role))] public string Role { get; set; } private string _password; [MinLength(8)] public string password { get => _password; set => _password = replacewithNull(value); } private string _confirmpassword; [Compare("password")] public string confirmpassword { get => _confirmpassword; set => _confirmpassword = replacewithNull(value); } private string replacewithNull(string value) { return string.IsNullOrEmpty(value) ? null : value; } } }
It contain validation , Business logic and database code
using Web_API_Tutorials.Entities; using Web_API_Tutorials.Models; namespace Web_API_Tutorials.Services { public interface IEmployee { void Employeecreate(CreateEmpRequest empRequest); void Employeeupdate(int id, UpdateEmpRequest empRequest); void Employeedelete(int id); IEnumerable<Employee> GetAll(); Employee EmployeeGetById(int id); } }
using AutoMapper; using Microsoft.EntityFrameworkCore; using Web_API_Tutorials.Entities; using Web_API_Tutorials.Helpers; using Web_API_Tutorials.Models; namespace Web_API_Tutorials.Services { public class EmployeeService : IEmployee { private DbContextData _dbContext; private readonly IMapper _mapper; public EmployeeService(DbContextData dbContext,IMapper mapper) { _dbContext = dbContext; _mapper = mapper; } public void Employeecreate(CreateEmpRequest empRequest) { var Emailexist = _dbContext.Employee.Any(e=>e.Email== empRequest.Email); if (Emailexist == true) { throw new AppException("Employee Email ID : '" + empRequest.Email + "' Already Exist"); } var emp = _mapper.Map<Employee>(empRequest); _dbContext.Add(emp); _dbContext.SaveChanges(); } public void Employeedelete(int id) { var emp = _dbContext.Employee.Find(id); if (emp == null) { throw new AppException("Employee Not Found"); } _dbContext.Remove(emp); _dbContext.SaveChanges(); } public Employee EmployeeGetById(int id) { var emp = _dbContext.Employee.Find(id); if (emp==null) { throw new AppException("Employee Not Found"); } return emp; } public void Employeeupdate(int id, UpdateEmpRequest empRequest) { var emp = EmployeeGetById(id); if (emp.Email==empRequest.Email) { throw new AppException("Employee Email ID : '" + empRequest.Email + "' Already Exist"); } _mapper.Map(empRequest,emp); _dbContext.Employee.Update(emp); _dbContext.SaveChanges(); } public IEnumerable<Employee> GetAll() { return _dbContext.Employee; } } }
using Microsoft.EntityFrameworkCore; using System.Text.Json.Serialization; using Web_API_Tutorials.Helpers; using Web_API_Tutorials.Services; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers().AddJsonOptions(json=> { json.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); json.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; }); builder.Services.AddDbContext<DbContextData>(con => con.UseSqlServer(builder.Configuration.GetConnectionString("connection"))); builder.Services.AddScoped<IEmployee, EmployeeService>(); builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); var app = builder.Build(); app.UseHttpsRedirection(); app.UseAuthorization(); app.UseMiddleware<MiddlewareErrorHandler>(); app.MapControllers(); app.Run();
Add-Migration 'Initial-Create' Update-Database
Controller is a class that handles requests to specific endpoints and returns HTTP response in the form of JSON or XML.
using AutoMapper; using Microsoft.AspNetCore.Mvc; using Web_API_Tutorials.Models; using Web_API_Tutorials.Services; namespace Web_API_Tutorials.Controllers { [Route("api/[controller]")] [ApiController] public class EmployeeRegController : ControllerBase { private readonly IEmployee _employee; private readonly IMapper _mapper; public EmployeeRegController(IEmployee employee, IMapper mapper) { _employee = employee; _mapper = mapper; } // GET: api/<EmployeeRegController> [HttpGet] public IActionResult Get() { var emp = _employee.GetAll(); return Ok(emp); } // GET api/<EmployeeRegController>/5 [HttpGet("{id}")] public IActionResult Get(int id) { var emp = _employee.EmployeeGetById(id); return Ok(emp); } // POST api/<EmployeeRegController> [HttpPost] public IActionResult Post(CreateEmpRequest createEmpmodel) { _employee.Employeecreate(createEmpmodel); return Ok(new { Message = "Employee Created" }); } // PUT api/<EmployeeRegController>/5 [HttpPut("{id}")] public IActionResult Put(int id, UpdateEmpRequest updateEmp) { _employee.Employeeupdate(id, updateEmp); return Ok(new { Message = "Employee Updated" }); } // DELETE api/<EmployeeRegController>/5 [HttpDelete("{id}")] public IActionResult Delete(int id) { _employee.Employeedelete(id); return Ok(new { Message = "Employee Deleted" }); } } }
Test the ASP.Net Core Web API Using Postman
The post How to Create .Net Core Web API – CRUD Operation appeared first on Tech Insights.
]]>