25
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Embed Size (px)

Citation preview

Page 1: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Introduction to ASP.NET MVC

Information for this presentation was taken fromPluralsight Building Applications with ASP.NET MVC 4

Page 2: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

ASP.NET MVC

Controller

Model View

/Home/About

Page 3: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Overview• Controllers

• Requests come into the controller from the routing engine• The requests specify a controller and an action method in the controller to execute

/Home/Index

• The Public methods inside of the controllers are actions• Typically the methods build models to be displayed in a view• Return ActionResults such as returning a View which can be used to display a model

• Views• Use the Razor markup syntax that lets you embed server-based code into web pages

• Models• Classes that describe your application data

Page 4: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Naming conventions for models, controllers, and views

• Model classes generally describe 'real-life' entities so call them by name• Product• Student

• Controllers add the Controller suffix• ProductController• StudentController

• Views are in the Views folder, and each folder in the Views folder matches the name of a controller, without the controller suffix. Each of those folders contain a .cshtml file with a name that corresponds to an action in the controller.

Page 5: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Controllers

• Routing• Routing rules deliver requests to the controller

• Controller Actions• Public methods on the controllers that have the ability to respond to an http request

from the web

• Action Filters• Introduce pre and post actions to a controller

• Action Parameters• Input data to actions

• Action Results• Output different types of results from the actions

Page 6: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Routes and Controllers

• Routing engine used to direct requests to the controllers

routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index",

id = UrlParameter.Optional } );

Page 7: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Actions

• Actions are public methods inside of a controller class• Action parameters are used to input data to an action• Action results typically return an ActionResult• Will utilize Action Selectors that are available• Will apply Action Filters

Page 8: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Results• Actions typically return an ActionResult

Page 9: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Action Selectors• ActionName• Specifies the action name for the method – used when you want to alias your

actions.

• AcceptVerbs• Specify with verb is allowed to reach an http action• HttpPost, HttpGet

Page 10: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Action Filters

• Action Filters apply pre and post action processing to an action and it’s result.• Used to apply crosscutting logic – logic that must execute across

multiple controller actions

Page 11: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Views

• Razor Syntax• HTML Helpers• Layout• Partial Views

Page 12: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Razor Templates• Razor View engine allows use to use Razor templates to product HTML

Page 13: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

C# code in Razor views

• @ means we are introducing C# code• Razor will automatically HTML encode any output sent though the @

sign to prevent cross site scripting attacks• Razor supports both code expressions and code blocks• Can use both C# and HTML in the code blocks

Page 14: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Layout with Razor• _Layout.cshtml takes the place of web forms Master pages• It uses inherited methods to specify content areas• RenderBody• RenderSection

Page 15: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

HTML Helpers• The purpose of an html helper is to make it easy to create small blocks of html• Create inputs• Create links• Create forms

Page 16: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Partial Views

• A partial view allows you to put html and C# code into a file that you can reuse across multiple other views, or use to simplify a view.• Create by adding a view and choosing the option to create a partial view.• Naming convention is to preface the partial view’s name with an _• Use the html helper Html.Partial to render the partial view• Pass in the name of the partial view and the model it needs

Page 17: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

The ADO.NET Entity Framework

Page 18: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Building Entities using Code First• Write your C# definitions• Set up a class which inherits from the DbContext

• Naming convention is to use the name of the database with Db as a suffix.• PetSuppliesDb

• The properties are typically DbSet<entity> which represents the entities that you want to query and persist.• public DbSet<ProductType> ProductTypes { get; set; }

• Since the class inherits for the DbContext class, when the application is run, it looks for a database, and if one is not found, creates one.

• Run the application to create the database.• By default, it is created in localdb with the same name as your data context class.• If you want to create the database somewhere else, modify your DefaultConnection in the

web.config and make a call to the base class constructor to use the DefaultConnection• public PetSupplies( ) : base(“name=DefaultConnection”) { }

• Set up a connection to the database in Server Explorer.

Page 19: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Entity Framework Migrations

• A feature of the Entity Framework which add the functionality to:• configure database schema using C# code, • seed your data using C# code • keep track of changes made to the entity classes

• keeps the database in sync with the changes made in the C# code

• Use the Package Manager Console (PowerShell command line tool)PM> Enable-Migrations -ContextTypeName PetSuppliesDbPM> Update-Database -Verbose

Page 20: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

LINQ

Page 21: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

View Model• A C# class in the Models folder used to aggregate information from

different places and different sources.• The View uses ViewModels to carry along information from a

controller request that a single entity model does not include.• It is not an entity that is created and it is not added as a Dbset that

will be included in the data context class to save in the database.

Page 22: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Bind Attributes• Alias with the Bind Attribute

Page 23: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Bind Attributes• Use the Bind attribute with exclude or include to define the fields you

want to exclude or include with the view.

Page 24: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Data Annotations - Validation• Use data annotations in your Entity classes to validate data.• Validation runs on both client and server side• Complete list: System.ComponentModel.DataAnnotations namespace

Page 25: Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4

Data Annotations – Display and DisplayFormat• Display annotations are used to change the way the data is displayed• Complete list: System.ComponentModel.DataAnnotations namespace