Академический Документы
Профессиональный Документы
Культура Документы
NET
Florin Olariu
“Alexandru Ioan Cuza”, University of Iași
Department of Computer Science
ASP.NET Core MVC
Agenda
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
@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
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