Introduction to NET Florin Olariu Alexandru Ioan Cuza






















![Model binding @model My. Model. Name [Http. Post] public IAction. Result My. Action(My. Model. Model binding @model My. Model. Name [Http. Post] public IAction. Result My. Action(My. Model.](https://slidetodoc.com/presentation_image_h2/8f9ad17fa6e0446f005c96b9006c9e4a/image-23.jpg)








![Filters [Authorize] public class Account. Controller : Controller { } Filters [Authorize] public class Account. Controller : Controller { }](https://slidetodoc.com/presentation_image_h2/8f9ad17fa6e0446f005c96b9006c9e4a/image-32.jpg)













- Slides: 45
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 View. Model 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 View. Component, View. Model 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. Map. Route(name: "Default", template: "{controller=Home}/ {action=Index}/{id? }");
Routing Attribute routing
Routing Attribute routing
Model binding
Model binding @model My. Model. Name
Model binding @model My. Model. Name <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 My. Model. Name [Http. Post] public IAction. Result My. Action(My. Model. Name model) { return View(model); }
Model validation
Model validation using System. Component. Model. Data. Annotations; public class My. Model. View. Model { [Required] public string Name { get; set; } [Required] public string Description { get; set; } }
Model validation public IAction. Result My. Action(My. Model. View. Model model) { if (Model. State. Is. Valid) { // work with the model } // If we got this far, something failed, redisplay form return View(model); }
Dependency Injection
Dependency Injection @inject Some. Service. Name <!DOCTYPE html> <head> <title>@Service. Name. Get. Title</title> </head> <body> <h 1>@Service. Name. Get. Title</h 1> </body> </html>
Filters
Filters
Filters using Filters. Sample. Helper; using Microsoft. Asp. Net. Core. Mvc. Filters; namespace Filters. Sample. Filters { public class Sample. Action. Filter : IAction. Filter { public void On. Action. Executing(Action. Executing. Context context) { // do something before the action executes } public void On. Action. Executed(Action. Executed. Context context) // do something after the action executes } } } {
Filters [Authorize] public class Account. Controller : 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>@Date. Time. Now</p>
Razor View Engine What is Razor? Rendering HTML Razor syntax <p>@Date. Time. 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!