Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4."— Presentation transcript:

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

2 ASP.NET MVC Controller ModelView /Home/About

3 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

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.

5 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

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

7 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

8 Results Actions typically return an ActionResult

9 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

10 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

11 Views Razor Syntax HTML Helpers Layout Partial Views

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

13 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

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

15 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

16 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

17 The ADO.NET Entity Framework

18 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 which represents the entities that you want to query and persist. public DbSet 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.

19 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 PetSuppliesDb PM> Update-Database -Verbose

20 LINQ

21 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.

22 Bind Attributes Alias with the Bind Attribute

23 Bind Attributes Use the Bind attribute with exclude or include to define the fields you want to exclude or include with the view.

24 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 namespaceSystem.ComponentModel.DataAnnotations namespace

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


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

Similar presentations


Ads by Google