Skip to main content

Posts

Showing posts with the label ASP.NET CORE MVC

WEB API in ASP.NET CORE MVC | EXPLAIN WEB API IN DEPTH by Shiva Sir

1️⃣ What is Web API? 🔹 Definition: A Web API (Web Application Programming Interface) is a set of HTTP endpoints exposed by a server application that allows other applications (frontend, mobile, etc.) to communicate over the web . In .NET Core, a Web API is a type of controller that returns data (usually JSON), not views (HTML). 2️⃣ Why Use Web API? Purpose Description 🔄 Communication Enables frontend and backend apps to communicate using HTTP ⚙️ Reusability Backend APIs can be used by web apps, mobile apps, desktop apps 🔐 Centralized Business Logic All logic is managed on the server side 🚀 Lightweight & Fast Returns raw data (JSON), which is faster than rendering views 3️⃣ Web API vs MVC Feature MVC Web API Output Returns Views (HTML) Returns Data (JSON/XML) Use Case Web applications (UI required) APIs for mobile, SPA, external systems Controller Type Controller ApiController Return Type IActionResult with View ActionResult<T> with data 4️⃣ Architecture of...

What is Middleware in ASP.NET Core MVC | Explain Middleware in depth

What is Middleware in ASP.NET Core MVC:   Middleware is a fundamental component in ASP.NET Core's request processing pipeline. Understanding it is essential for customizing how HTTP requests and responses are handled in your application. Each middleware component: Receives the HTTP request . Performs some processing . Optionally calls the next middleware in the pipeline. Optionally modifies the HTTP response . Middleware in ASP.NET Core Pipeline The middleware pipeline is configured in Startup.cs (or Program.cs in .NET 6+). For .NET Core 3.1/ASP.NET Core MVC: public void Configure ( IApplicationBuilder app, IWebHostEnvironment env ) { // Built-in middleware examples app.UseDeveloperExceptionPage(); // 1. Show developer error page app.UseStaticFiles(); // 2. Serve static files (CSS, JS) app.UseRouting(); // 3. Enable routing app.UseAuthentication(); // 4. Handle authentication app...

What is Dependency Injection in ASP.NET Core MVC

  What is Dependency Injection (DI)? Dependency Injection (DI) is a design pattern used to manage dependencies between different parts of your application. A dependency is simply an object that another object relies on to do its job. DI is a way to provide those dependencies from the outside, rather than letting a class create them internally. 🤔 Why Use DI? Without DI: public class HomeController {     private TimeService _timeService = new TimeService(); // tightly coupled     public string GetTime()     {         return _timeService.GetCurrentTime();     } } ❌ The controller is directly creating the dependency. ❌ Hard to test (can’t easily replace TimeService with a mock). ❌ If the implementation changes, you have to change the controller code. public class HomeController {     private readonly ITimeService _timeService;     public HomeController(ITimeService timeService)     {   ...

LINQ to ASP.NET CORE MVC by Shiva Sir

 🔷 What is LINQ? LINQ (Language Integrated Query) is a .NET feature that allows you to query collections (like arrays, lists, and database entities) in a SQL-like or method-chaining syntax directly within C# code. 🔷 Types of LINQ Syntax: Query Syntax (SQL-like) Method Syntax (Fluent/Chaining) Mixed Syntax (Less common) 🔷 Common LINQ Operations Operation Example (Method Syntax) Where .Where(x => x.Age > 18) Select .Select(x => x.Name) OrderBy .OrderBy(x => x.Name) GroupBy .GroupBy(x => x.Department) Join .Join(..., ..., ..., ...) Any, All, Count, FirstOrDefault, Take, Skip, etc. 🔷 ASP.NET Core MVC Example: Student Management System Step 1: Setup Entity Models/Student.cs public class Student {     public int Id { get; set; }     public string Name { get; set; }     public int Marks { get; set; }     public string Subject { get; set; } } Step 2: Create a Fake List for LINQ Demo Controllers/StudentController.cs us...

Razor form example in ASP.NET Core MVC

Complete Razor form example in ASP.NET Core MVC demonstrating all major form elements: How to Create Form Element in Razor Form Type Element Used Textbox <input asp-for="Name" /> Radio Button <input type="radio" asp-for="Gender" /> Checkbox <input type="checkbox" asp-for="IsSubscribed" /> ListBox <select multiple asp-for="SelectedSkills" /> DropdownList <select asp-for="SelectedCountry" />  Create Registration Form using Razor Form Elements: Create Model: UserFormModel.cs public class UserFormModel {     public string Name { get; set; }     public string Gender { get; set; }     public bool IsSubscribed { get; set; }     public List<string> SelectedSkills { get; set; }     public string SelectedCountry { get; set; }     public List<string> Countries { get; set; }     public List<string> Skills { get; set; } } Create Controller: FormController.cs using Mi...

Code First Approach in .NET Core MVC

Code First Approach in ASP.NET Core MVC? Code First is complete dynamic approach of entity framework where we create database configuration and table dynamically using dB Context class.  1)  Create Web Project using ASP.NET Core MVC Project Template 2)  Add Library from Nuget Package EntityFrameworkCore.Design EntityFrameworkCore.SQLServer EntityFrameworkCore.Tools 3) Create Local Database using Server Explorer 4)  Create Model Class Like this using System.ComponentModel.DataAnnotations; namespace WebApplication3.Models {     public class Employee     {         [Key]         public int Id { get; set; }         public string Name { get; set; }             public string Email { get; set; }         public string Mobileno { get; set; }     } } 5)  Create DbContext class using Microsoft.EntityFrameworkCore; namespace WebAppl...

Database connectivity in ASP.net core , .NET Core DB Connection, step by step

Database connectivity in ASP.net core , .NET Core DB Connection, step by step  Step1st: Create ASP.NET Core Project Step2nd: Manage Nuget Package Manager Console  Microsoft.EntityFrameworkCore 7.0 Microsoft.EntityFrameworkCore.SqlServer 7.0 Microsoft.EntityFrameworkCore.Tools 7.0 Step3rd: Create Model Class namespace WelcomeProject.Models {     public class Product     {         public int Id { get; set; }         public string Name { get; set; }         public decimal Price { get; set; }     } } Create AppDbContext Class also under model using Microsoft.EntityFrameworkCore; using System.Collections.Generic; namespace WelcomeProject.Models {     public class AppDbContext : DbContext     {         public DbSet<Product> Products { get; set; }         private readonly IConfiguration _configuration;         ...

How to upload file in ASP.NET Core MVC, File uploading concept in .net core MVC

 How to  upload file in  ASP.NET Core MVC, File uploading concept in .NET  core MVC:- It is used to upload any external content such as audio, video, text and doc file under project. Microsoft provide IFormFile class to handle file uploading operation Step by Step Implementation 1)  Create Project 2)  Create FileUpload.cs class under Models folder public class FileUpload     {         public IFormFile FormFile { set; get; }     } 3)  Create FileUploadingController Class using Microsoft.AspNetCore.Mvc; using CodeFirstExample.Models; using Microsoft.Extensions.Hosting.Internal; namespace CodeFirstExample.Controllers {     public class FileUploadController : Controller     {         private IWebHostEnvironment Environment;         public FileUploadController(IWebHostEnvironment _environment)         {         ...

Code First Approach in ASP.NET CORE MVC

 How to implement Code First Approach in ASP.NET CORE MVC:- Code First Approach is best for light-weight and smaller project in ASP.NET MVC Core, it is basically created to those developer who is not know about SQL. without SQL Server or any database package developer can create complete project. Step 1 New Project Once you have installed the Visual Studio 2019 and .Net Core 3.1 SDK, you can start building a new ASP.NET Core Application. Step 2nd:- Create a web app and From the Visual Studio select Create a new project. Select ASP.NET Core Web Application > Next. Step 3rd:- Give your project a name i.e. SampleCodeFirstExample and give your project a location where it’ll be saved and click Create. Step4th:-  Install the NuGet Packages From the Solution right click on dependency, select Manage NuGet Packages and Select the Browse tab, and then enter Microsoft.EntityFrameworkCore.SqlServer in the search box, select Latest Stable Version and Install. Step5h:- Now, install ...

How to perform CRUD Operation in .NET CORE using Database First Approach:- BY Shiva Sir

 How to perform CRUD Operation in .NET CORE using Database First Approach:-  BY Shiva Sir:- Database First means you should create database and table from Database end and Program Code and Business Logic from application end. Database first most useful approach in Entity framework for large projects. 1)  Create Database  2)  Create Table 3)  Create .NET MVC Project 4)  MICROSOFT.ENTITYFRAMEWORKCORE.SqlServer MICROSOFT.ENTITYFRAMEWORKCORE.TOOLS MICROSOFT.ENTITYFRAMEWORK.CORE 5)  WRITE THIS COMMAND to GENERATE CONTEXT CLASS AND MODEL CLASS. Scaffold-DbContext "Data Source=(localdb)\ProjectsV13;Initial Catalog=Northwind; Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;"  -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models/DB 6)  go into appsetting.json file and create connection String {   "Logging": {     "LogLevel": {       "Default": "Information", ...