Introduction to .
NET
Florin Olariu
“Alexandru Ioan Cuza”, University of Iași
Department of Computer Science
ASP.NET Core MVC
Agenda
Short recap about MVC
Understanding MVC
Routing
Model binding
Model validation
Dependency injection
Filters
Razor View Engine
Bringing all together
Summary
Short recap about MVC
Short recap about MVC
Short recap about MVC
Models and their responsibilities
Short recap about MVC
Models and their responsibilities
The Model in an MVC application represents the state of the application
Short recap about MVC
Models and their responsibilities
The Model in an MVC application represents the state of the application
Business logic or operations that should be performed by models
Short recap about MVC
Models and their responsibilities
The Model in an MVC application represents the state of the application
Business logic or operations that should be performed by models
Strongly-typed views will typically use ViewModel types specifically designed to
contain the data to display on that view
Short recap about MVC
Views and their responsibilities
Short recap about MVC
Views and their responsibilities
Used for presenting content through the user interface
Short recap about MVC
Views and their responsibilities
Used for presenting content through the user interface
They use the Razor view engine to embed .NET code in HTML markup.
Short recap about MVC
Views and their responsibilities
Used for presenting content through the user interface.
They use the Razor view engine to embed .NET code in HTML markup.
If we need to deal with logic and display complex data we should consider using
ViewComponent, ViewModel or View template
Short recap about MVC
Controllers and their responsibilities
Short recap about MVC
Controllers and their responsibilities
Handles user interaction, work with model and than select a view that will be
rendered
Routing
Routing
routes.MapRoute(name: "Default",
template: "{controller=Home}/
{action=Index}/{id?}");
Routing
Attribute routing
Routing
Attribute routing
Model binding
Model binding
@model MyModelName
Model binding
@model MyModelName
<form asp-controller="Product" asp-action="Edit" method ="post" >
<label asp-for="Name"></label >
<input asp-for="Name"/>
<label asp-for="Description"></label >
<input asp-for="Description"/>
<input type="submit" value="Submit" />
</form>
Model binding
@model MyModelName
[HttpPost]
public IActionResult MyAction(MyModelName model)
{
return View(model);
}
Model validation
Model validation
using System.ComponentModel.DataAnnotations;
public class MyModelViewModel
{
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
}
Model validation
public IActionResult MyAction(MyModelViewModel model)
{
if (ModelState.IsValid)
{
// work with the model
}
// If we got this far, something failed, redisplay form
return View(model);
}
Dependency Injection
Dependency Injection
@inject SomeService ServiceName
<!DOCTYPE html>
<html>
<head>
<title>@ServiceName.GetTitle</title>
</head>
<body>
<h1>@ServiceName.GetTitle</h1>
</body>
</html>
Filters
Filters
Filters
using FiltersSample.Helper;
using Microsoft.AspNetCore.Mvc.Filters;
namespace FiltersSample.Filters {
public class SampleActionFilter : IActionFilter {
public void OnActionExecuting(ActionExecutingContext context) {
// do something before the action executes
}
public void OnActionExecuted(ActionExecutedContext context) {
// do something after the action executes
}
}
}
Filters
[Authorize]
public class AccountController : Controller
{
}
Razor View Engine
Razor View Engine
What is Razor?
Razor View Engine
What is Razor?
Rendering HTML
Razor View Engine
What is Razor?
Rendering HTML
<p>Hello World</p>
Razor View Engine
What is Razor?
Rendering HTML
Razor syntax
<p>@DateTime.Now</p>
Razor View Engine
What is Razor?
Rendering HTML
Razor syntax
<p>@DateTime.Now</p>
@{
var joe = new Person("Joe", 33);
}
<p>Age@(joe.Age)</p>
@for (var i = 0; i < people.Length; i++)
{
var person = people[i];
@:Name: @person.Name
}
Bringing all together
DEMO ~ 40 minutes
Building an MVC Core application from scratch
Scaffolding, validation, and model binding
We will create a movie with the entire flow and explanations
One more thing…(1/2)
One more thing…(2/2)
Postel’s Law:
“Be conservative in what you do,
be liberal in what you accept
from others.”
Summary
Models
Views
Controllers
Razor
Filters
Validations
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
https://docs.microsoft.com/en-us/aspnet/core/mvc/overview
http://deviq.com/kinds-of-models/
Questions
Do you have any other questions?
Thanks!
See you next time!