Professional Documents
Culture Documents
NET
Florin Olariu
“Alexandru Ioan Cuza”, University of Iași
Department of Computer Science
ASP.NET Core MVC
Agenda
PROS CONS
Helps with adhering to the DI introduces a learning curve for
Dependency Inversion Principle (DIP) some developers
Allows objects to be easily swapped DI may require a significant overhaul
with replacements of existing projects
Facilitates the use of the Strategy Project timelines may not allow DI
Design Pattern (SDP)
Improves the testability of
applications
Enables loose coupling of software
components
Using Dependency Injection with
ASP.NET(5/12)
IoC/DI mechanisms
Using Dependency Injection with
ASP.NET(6/12)
IoC/DI mechanisms
Transient
Using Dependency Injection with
ASP.NET(7/12)
IoC/DI mechanisms
Transient
Scoped
Using Dependency Injection with
ASP.NET(8/12)
IoC/DI mechanisms
Transient
Scoped
Singleton
Using Dependency Injection with
ASP.NET(9/12)
IoC/DI mechanisms
Transient
Scoped
Singleton
Instance (special case of Singleton)
Using Dependency Injection with
ASP.NET(10/12)
IoC/DI mechanisms
Transient
Scoped
Singleton
Instance (special case of Singleton)
Using Dependency Injection with
ASP.NET(11/12)
IoC/DI mechanisms
Transient
Scoped
Singleton
Instance (special case of Singleton)
Constructor injection
Using Dependency Injection with
ASP.NET(11/12)
IoC/DI mechanisms
Transient
Scoped
Singleton
Instance (special case of Singleton)
Constructor injection
Action injection
Using Dependency Injection with
ASP.NET(12/12)
DEMO
Using controllers and views
Using IoC and DI
Verifying routes
Creating midlleware
Models, views and controllers – an MVC
refresher(1/13)
Models, views and controllers – an MVC
refresher(2/13)
Models, views and controllers – an MVC
refresher(3/)
Controllers
Models, views and controllers – an MVC
refresher(4/13)
Controllers
Is a subclass of the base class Controller
Models, views and controllers – an MVC
refresher(5/13)
Controllers
Is a subclass of the base class Controller
Lives into the following namsespace: Microsoft.AspNetCore.Mvc
Models, views and controllers – an MVC
refresher(6/13)
Controllers
Is a subclass of the base class Controller
Lives into the following namsespace: Microsoft.AspNetCore.Mvc
Typically a controller returns an IActionResult from its action methods
Models, views and controllers – an MVC
refresher(7/13)
Models
Models, views and controllers – an MVC
refresher(8/13)
Models
Typically a model is DTO class (properties)
Models, views and controllers – an MVC
refresher(9/13)
Models
Typically a model is DTO class (properties)
For a cleaner architecture, you can use a view-specific model (or ViewModel) to
bind to a view.
Models, views and controllers – an MVC
refresher(10/13)
Views
Models, views and controllers – an MVC
refresher(11/13)
Views
Views are stored in .cshtml files
Models, views and controllers – an MVC
refresher(12/13)
Views
Views are stored in .cshtml files
ViewBag allows you to store your own properties and display them in the view.
Models, views and controllers – an MVC
refresher(13/13)
Views
Views are stored in .cshtml files
ViewBag allows you to store your own properties and display them in the view.
We can use tag helpers in your views for smoother syntax.
Web configuration with
project.json(1/3)
Web configuration with
project.json(2/3)
userSecretsId: A GUID-like value used for working with user secrets
dependencies: A list of server-side dependencies
version: The project version
frameworks: .NET Core framework name and version
build/runtime/publish options: Build/runtime configuration and a list of
files/ folders to include when publishing the web app
scripts: A list of scripts for actions that can be triggered by specific events,
such as prepublish, postpublish, and others
Web configuration with
project.json(3/3)
Understanding MVC(1/4)
Understanding MVC(2/4)
Implementing controllers
HttpGet: Uses the HTTP GET method with optional querystring parameters
HttpPost: Uses the HTTP POST method for form submissions to create an entity
Understanding MVC(3/4)
Implementing controllers
HttpGet: Uses the HTTP GET method with optional querystring parameters
HttpPost: Uses the HTTP POST method for form submissions to create an entity
Implementing controllers
DEMO
Understanding MVC(1/4)
Implementing views
Understanding MVC(2/4)
Implementing views
ViewData, ViewBag, and TempData
Understanding MVC(3/4)
Implementing views
ViewData, ViewBag, and TempData
ViewData[" PatientId"] = id;
ViewBag.PatientData = "someData";
TempData[" UserToken"] = userTokenData;
@{
ViewData["Title"] = "Patient Details";
}
<h2> Patient Details </h2>
<ul>
<li> ID: @ViewData[" PatientId"] </li>
<li> Name: @ViewData[" PatientName"] </li>
</ul>
Understanding MVC(4/4)
Implementing views
ViewData, ViewBag, and TempData
@{
ViewData[" Title"] = "Patient Index";
}
<h2> Patient Index, with Tag Helpers </h2>
<ul> @for (int i=0; i<10; i++) {
<li>
<a asp-controller =" Patient"
asp-action ="Details" asp-route-id ="@i"
asp-route-name ="Patient @i" >
Patient # @i </a></li>
}
</ul>
Understanding MVC(1/6)
DEMO
One more thing…(1/2)
One more thing…(2/2)