.
NET Core:
Introduction to .NET Core:
It's a modular framework that
.NET Core is an open-source, ASP.NET is a subset of .NET
allows developers to use only
cross-platform framework for Core specifically designed for Benefits and Features of .NET
the components they need,
building modern, high- building web applications and Core:
making applications more
performance applications. services.
lightweight.
Cross-Platform High Performance: .NET Core is Modular and Lightweight:
Open-Source: .NET Core is
Compatibility: .NET Core runs optimized for performance, Developers can choose and
open-source, allowing
on Windows, macOS, and Linux, offering faster startup times and include only the necessary
community contributions and
allowing developers to build improved runtime efficiency components, reducing the size
providing transparency into the
and deploy applications on any compared to previous versions of applications and improving
framework's development.
platform. of .NET. deployment efficiency.
ASP.NET Core: Features and
Advantages:
• ASP.NET Core is a cross-platform, high-performance framework for building web
applications and services.
• Features:
• Modularity: ASP.NET Core is modular, allowing developers to include only the necessary
components for their applications.
• Cross-Platform: Like .NET Core, ASP.NET Core runs on Windows, macOS, and Linux.
• Integrated Dependency Injection: Built-in dependency injection container simplifies managing
dependencies and promotes loose coupling.
• Middleware Pipeline: ASP.NET Core uses a middleware pipeline for processing HTTP requests,
providing flexibility and extensibility.
• Unified MVC and Web API Framework: ASP.NET Core combines MVC and Web API into a
single programming model, simplifying the development of both web pages and web APIs.
• Cross-Site Request Forgery (CSRF) Protection: Built-in protection against CSRF attacks to
enhance security.
Application and Request Life Cycle in ASP.NET
Core MVC:
• ASP.NET Core MVC follows a request processing pipeline that handles
incoming HTTP requests and generates corresponding responses.
• Life Cycle:
• Routing: Incoming requests are matched to controller actions based on route
definitions.
• Model Binding: Request data is bound to action method parameters.
• Action Execution: The controller action method is invoked to process the
request.
• Result Execution: The action result (view, JSON, etc.) is executed to generate
the response.
• Response: The response is sent back to the client.
• // Startupcs
Routing: • . public void Configure(IApplicationBuilder app,
IWebHostEnvironment env)
• {
• if (env.IsDevelopment())
• {
• app.UseDeveloperExceptionPage();
• } else
• {
• Define route patterns in • app.UseExceptionHandler("/Home/Error");
the Startup.cs file's •
• }
app.UseHsts();
Configure method using • app.UseHttpsRedirection();
the UseEndpoints • app.UseStaticFiles();
extension method. • app.UseRouting();
• Configure routes to map • app.UseAuthorization();
incoming requests to • app.UseEndpoints(endpoints =>
specific controller actions. •
•
{
endpoints.MapControllerRoute(
• name: "default",
• pattern: "{controller=Home}/{action=Index}/{id?}");
• // HomeController.cs
2. Model Binding: • public class HomeController : Controller
• {
• public IActionResult Index()
• Define action methods in • {
controllers that expect • return View();
parameters. • }
• ASP.NET Core automatically binds
request data to action method • public IActionResult Welcome(string
parameters based on parameter name)
names and types. • {
• ViewData["Message"] = $"Hello,
{name}!";
• return View();
• }
• }
• public IActionResult Index()
•{
3. Action Execution:
• var products =
_context.Products.ToList(); //
Data access
• Implement action methods in • return View(products); //
controllers to handle incoming
requests.
Send data to the view
• Controller actions perform necessary •}
processing based on the request and
return an action result.
What happens here?
• Business logic
• Database queries (via Entity
Framework Core)
• Preparing the response
• return View(product); // Sends
product to a View
4:Result Execution
• ASP.NET Core automatically handles
sending the generated response to the /Views/Product/Details.cshtml.
client.
• <h1>@Model.Name</h1>
• Responses can include HTML content,
JSON data, files, etc., based on the action • <p>Price: @Model.Price</p>
result returned by the controller.
• Types of Action Results:
• ViewResult: returns a .cshtml page
• JsonResult: returns JSON data
• RedirectToActionResult: redirects to
another action
Respons • Instructions:
• Create views corresponding
• <!-- Welcome.cshtml -->
• @{
e to action results. • ViewData["Title"] =
• Render the appropriate view "Welcome";
based on the action result • }
returned by the controller.
• <h2>Welcome</h2>
• <p>@ViewData["Message
"]</p>
Visual
• Browser Request
• ↓
• Routing (URL → Controller/Action)
• ↓
• Model Binding (URL/Form → Parameters)
• ↓
• Action Execution (Business Logic)
• ↓
• Result Execution (View/JSON Response)
• ↓
• Client Receives Response