You are on page 1of 51

Introduction to .

NET
Florin Olariu
“Alexandru Ioan Cuza”, University of Iași
Department of Computer Science
ASP.NET Core MVC
Agenda

 IoC/DI mechanism(part II)


 Models, views and controllers – an MVC refresher
 Controllers
 Models
 Views
 Web configuration with project.json
 Understanding MVC
 Implementing controllers
 Implementing views
 Implementing models and viewmodels
Using Dependency Injection with
ASP.NET(1/12)
 IoC/DI mechanisms
Using Dependency Injection with
ASP.NET(2/12)
 IoC/DI mechanisms
Using Dependency Injection with
ASP.NET(3/12)
 IoC/DI mechanisms
Using Dependency Injection with
ASP.NET(4/12)
 IoC/DI mechanisms

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

 HttpPut: Uses the HTTP PUT method to edit an existing entity


 HttpDelete: Uses the HTTP DELETE method to delete an existing entity
 HttpPatch: Allows partial model updates instead of a full PUT request
 AcceptVerbs: Allows multiple action verbs to be specified
Understanding MVC(4/4)

 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)

 Implementing models and ViewModels


Understanding MVC(2/6)

 Implementing models and ViewModels


 A model is just a class file with a .cs file extension.
Understanding MVC(3/6)

 Implementing models and ViewModels


 A model is just a class file with a .cs file extension.
Understanding MVC(4/6)

 Implementing models and ViewModels


 A model is just a class file with a .cs file extension.
Understanding MVC(5/6)

 Implementing models and ViewModels


 A model is just a class file with a .cs file extension.
Understanding MVC(6/6)

 Implementing models and ViewModels

 DEMO
One more thing…(1/2)
One more thing…(2/2)

“In summary, ASP.NET MVC gives developers the choice of shooting


themselves in the foot, although it's certainly possible to do it cleanly.
As such, it is suited for developers who are comfortable with web
development and have a clean style, but it is not for the developers who
want Widgetized behavior a la winforms without having to delve too deeply
into the markup.”
Bibliography

 https://docs.asp.net/en/latest/tutorials/first-web-api.html - By Mike Wasson


 and Rick Anderson
 Nagel, Christian. Professional C# 6 and .NET Core 1.0
 Chowdhuri, Shahed. ASP.NET Core Essentials
Questions

 Do you have any other questions?


Thanks!
See you next time! 

You might also like