0% found this document useful (0 votes)
37 views37 pages

Day 5 ASP - Net Core MVC

The document outlines an agenda for a meeting on ASP.Net Core MVC, covering topics such as project setup, MVC architecture, dependency injection, and layered architecture. It includes details on tools needed, the .NET Core roadmap, and various MVC components like models, views, and controllers. Additionally, it discusses Razor Pages, action results, and the advantages of using strongly typed views and view models.

Uploaded by

jimcyabrahm0.17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views37 pages

Day 5 ASP - Net Core MVC

The document outlines an agenda for a meeting on ASP.Net Core MVC, covering topics such as project setup, MVC architecture, dependency injection, and layered architecture. It includes details on tools needed, the .NET Core roadmap, and various MVC components like models, views, and controllers. Additionally, it discusses Razor Pages, action results, and the advantages of using strongly typed views and view models.

Uploaded by

jimcyabrahm0.17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Agenda on 23-11-2023

 ASP.Net Core MVC


 MVC Project set up
 Project structure
 Models, Views and Controllers,
 View Model
 Dependency Injection
 Layered Architecture in MVC in Single Project
 Onion Architecture in MVC in Multiple Projects
ASP.NET CORE MVC
TOOLS NEEDED

VISUAL SSMS
.NET 6 STUDIO
2022 2018
.NET CORE ROADMAP

.NET CORE .NET CORE .NET CORE .NET CORE .NET CORE .NET CORE

1.0 2.0 3.0 3.1 5.0 6.0


June 2016 Aug 2018 Sept 2019 Dec 2019 Nov 2020 Nov 2021
BUILT IN
DEPENDENCY
INJECTION

CROSS EASY
PLATFORM UPDATES

ASP.NET CORE
FAST AND
OPEN CLOUD
SOURCE FRIENDLY

PERFORMANCE
.NET CORE
● .NET Core is an open-source, cross-platform framework for
building modern, cloud-based, and internet-connected
applications.Introduction to .NET Core

● Why:
● Cross-platform compatibility.
● High performance.
● Modular and lightweight.
DEPENDENCY
INJECTION
Dependency Injection
● Dependency Injection is a technique where dependencies
are injected into a class rather than created within it.

● Why:
● Promotes loose coupling.
● Easier unit testing.
● Better maintainability.
WITHOUT DEPENDENCY INJECTION
Page 1
Db dbObj = new Db()
Email

Email emailObj = new Email()

Page 2
Email emailObj = new Email()

Db dbObj = new Db()

Page 3
Email emailObj = new Email()
Database
Db dbObj = new Db()
WITH DEPENDENCY INJECTION
Page 1
IDb
Email
IEmail

IEmail,Email

IEmail Page 2
DI
CONTAINER

IDb
IDb,Db

Page 3
IEmail

Database

IDb
Dependency Injection Example
● public void ConfigureServices(IServiceCollection services)
● {
● services.AddTransient<IEmployeeService, EmployeeService>();
● }
MVC
● Models: Represent the data and business logic of the
application.
● Views: Display information to the user.
● Controllers: Handle user input and interact with the model.

● Why:
● Clear separation of concerns.
● Easy maintenance and scalability.
.NET CORE PIPELINE

Request from Browser

Static
Auth MVC
Files
MVC ARCHITECTURE

2. Get Data
3. Get Presentation

USER 1. Request
CLICKS

4. Response
ROUTING IN MVC

The URL pattern for routing is considered after the domain name.
● https://localhost:55555/Category/Index/3
● https://localhost:55555/{controller}/{action}/{id}

URL Controller Action Id

https://localhost:55555/Category/Index Category Index Null

https://localhost:55555/Category Category Index Null

https://localhost:55555/Category/Edit/3 Category Edit 3

https://localhost:55555/Product/Details/3 Product Details 3


ENDPOINTS

https://localhost:1234/Home/Index
MVC /{controller}/{action}

RAZOR PAGES https://localhost:1234/Home/Login

SIGNALR https://localhost:1234/Hub/Pop
APPSETTINGS.JSON

• All of the application’s settings are contained in a


file named as appsettings.json
• Any changes to the appsettings.json file will require
restarting the “Microsoft IIS Administration” service
to take effect.
TAG HELPERS

• Tag Helpers are introduced with ASP.NET Core


• Tag Helpers enable server-side code to participate
in creating and rendering HTML elements in Razor
files.
• Tag helpers are a new feature and similar to HTML
helpers, which help us render HTML.
• They are more focused around HTML tags and
much more easier to use with HTML.
PRODUCT

CATEGORY APPLICATIONTYPE
Tag Helpers Example
Razor Pages
• Introduced in asp.net core 2.0
• Razor Pages is a new feature of ASP.NET Core MVC
that makes coding page-focused scenarios easier and
more productive
• Razor pages is not just for simple scenarios, everything
that you can do with MVC you can do by using Razor
pages like Routing, Models, ActionResult, Tag Helpers
and so on.
Routing in Razor Pages
• Routing in Asp.net Razor pages maps URL’s to Physical
file on disk.
• Razor pages needs a root folder.
• Index.cshtml is a default document

URL Maps To
www.domain.com /Pages/Index.cshtml
www.domain.com/index /Pages/Index.cshtml
www.domain.com/account /Pages/account.cshtml
/Pages/account/index.cshtml
Action Result

• ActionResult is a result of action methods/pages or


return types of action methods/page handlers
• Action result is a parent class for many of the derived
classes that have associated helpers.
• The IActionResult return type is appropriate when
multiple ActionResult return types are possible in an
action.
Action Result in Razor Pages
ActionResult Helper Description

ContentResult Content Takes a string and returns it with a text/plaincontent-type header by


default. Overloads enable you to specify the content-type to return other
formats such as text/html or application/json, for example.

FileContentResult File Returns a file from a byte array, stream or virtual path.

NotFoundResult NotFound Returns an HTTP 404 (Not Found) status code indicating that the
requested resource could not be found.

PageResult Page Will process and return the result of the current page.

2
PartialResult Partial Returns a Partial Page.

RedirectToPageResult RedirectToPage Redirects the user to the specified page.


RedirectToPagePermanent
RedirectToPagePreserveMethod
RedirectToPagePreserveMethodPermanent

ViewComponentResult Returns the result of executing a ViewComponent.


Action Result in MVC
Action Result Helper Method Description

ViewResult View Renders a view as a Web page.

PartialViewResult PartialView Renders a partial view, which defines a section of a view that can be rendered inside another view.

RedirectResult Redirect Redirects to another action method by using its URL.

RedirectToRouteResult RedirectToAction Redirects to another action method.


RedirectToRoute

ContentResult Content Returns a user-defined content type.

JsonResult Json Returns a serialized JSON object.

JavaScriptResult JavaScript Returns a script that can be executed on the client.

FileResult File Returns binary output to write to the response.

EmptyResult (None) Represents a return value that is used if the action method must return a null result (void).
VIEWBAG VIEWDATA TEMPDATA
● ViewBag transfers data from the ● ViewData transfers data from ● TempData can be used to store
Controller to View, not vice-versa. the Controller to View, not vice- data between two consecutive
Ideal for situations in which the versa. Ideal for situations in which requests.
temporary data is not in a model. the temporary data is not in a ● TempData internaly use
● ViewBag is a dynamic property model. Session to store the data. So think
that takes advantage of the new
● ViewData is derived from of it as a short lived session.
dynamic features in C# 4.0
ViewDataDictionary which is a ● TempData value must be type
● Any number of properties and dictionary type.
values can be assigned to ViewBag
cast before use. Check for null
● ViewData value must be type values to avoid runtime error.
● The ViewBag's life only lasts cast before use.
during the current http request. ● TempData can be used to store
ViewBag values will be null if ● The ViewData's life only lasts only one time messages like error
redirection occurs. during the current http request. messages, validation messages.
ViewData values will be null if
● ViewBag is actually a wrapper
redirection occurs.
around ViewData.

ViewBag internally inserts data into ViewData dictionary. So the key of ViewData
and property of ViewBag must NOT match
STRONGLY
TYPED VIEWS
STRONGLY TYPE

• A strongly typed view is a view that is strongly typed to


a model or a view model. It means that the view has a
direct reference to a model or a view model class, and
the view can access the properties and methods of that
class.
• To create a strongly typed view in ASP.NET Core MVC,
you need to specify the model or view model type in
the view file using the @model directive
Example
• if you have a model called “Employee", you can specify
it in the view like:

@model MyApp.Models.Employee

<h1>@Model.Name</h1>
<p>@Model.Address</p>

In this example, the view is strongly typed to the


“Employee" model, and you can access its properties like
"Name" and "Address" using the "@Model" keyword.
Advantages
A strongly typed view provides several benefits:
o Improved type safety: When you use a strongly typed view, the
compiler checks the types of the model or view model properties
and catches any errors at compile-time rather than at runtime.
o Improved IntelliSense: With a strongly typed view, you can take
advantage of IntelliSense to quickly and easily access the
properties and methods of the model or view model.
o Improved maintainability: By using a strongly typed view, you can
easily identify where the model or view model is used in the view,
which makes it easier to maintain the code in the future.
VIEWMODELS
VIEWMODEL
• ViewModel contain fields that are represented in the view

• ViewModel can have specific validation rules using data


annotations.

• ViewModel can have multiple entities or objects from


different data models or data source.

• ViewModel helps to implement strongly typed views.


• To create a ViewModel in ASP.NET Core MVC, you need
to create a class that contains the data that the view
needs. Typically, you will create a new class in the
Models folder of your project. Then, you can pass the
ViewModel to the view by using the @model directive,
just like you would with a regular model.
Advantages
Simplified view logic: By using a ViewModel, you can
simplify the view logic by providing only the necessary
data to the view, rather than all the data in the model.

Improved testability: ViewModel classes are simple


classes that contain only data, so they are easy to test.

Better separation of concerns: ViewModel classes help to


separate the view from the model and keep the
presentation logic out of the model.
Layered Architecture
● Layered Architecture involves organizing your application
into different layers (presentation, business logic, data
access) for better maintainability..

● Why:
● Separation of concerns.
● Scalability and maintainability.
Layered Architecture
● /MyMvcApp
● /Controllers
EmployeeController.cs
● /BusinessLogicLayer
● EmployeeService.cs
● /DataAccessLayer
● EmployeeRepository.cs
● /Models
● Employee.cs
Thank you

You might also like