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

Post on 30-Dec-2015

224 views 3 download

transcript

Introduction to ASP.NET MVC

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

ASP.NET MVC

Controller

Model View

/Home/About

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

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.

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

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 } );

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

Results• Actions typically return an ActionResult

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

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

Views

• Razor Syntax• HTML Helpers• Layout• Partial Views

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

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

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

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

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

The ADO.NET Entity Framework

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.

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

LINQ

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.

Bind Attributes• Alias with the Bind Attribute

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

want to exclude or include with the view.

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

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