Introduction to ASP NET MVC ASP NET MVC

  • Slides: 16
Download presentation
Introduction to ASP. NET MVC

Introduction to ASP. NET MVC

ASP. NET MVC /Home/About Model Controller View

ASP. NET MVC /Home/About Model Controller View

Overview • Controllers • Requests come into the controller from the routing engine •

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 Action. Results 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

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 • Products. Controller • Students. Controller • 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 rules deliver requests to the controller • Controller Actions • Public

Controllers • Routing rules deliver requests to the controller • Controller Actions • Public methods on the controllers that can 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.

Routes and Controllers • Routing engine used to direct requests to the controllers routes. Map. Route( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = Url. Parameter. Optional } );

Actions • Actions are public methods inside of a controller class • Action parameters

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 Action. Result • Will utilize Action Selectors that are available • Will apply Action Filters

Action. Result • Actions typically return an Action. Result • There are three categories

Action. Result • Actions typically return an Action. Result • There are three categories of data types of Action. Result • Content Returning Results • Redirection Results • Status Results

Action Selectors • Action. Name • Specifies the action name for the method –

Action Selectors • Action. Name • Specifies the action name for the method – used when you want to alias your actions. • Accept. Verbs • Specify with verb is allowed to reach an http action • Http. Post, Http. Get [Action. Name("Find")] [Http. Get] public Action. Result Get. By. Id(int id) { //get from database return View( ); } [Action. Name(“Modify”)] [Http. Post] Public Action. Result Edit(string department. Name) { //change department name return View( ); }

Action Filters • An action filter is an attribute that modifies the way in

Action Filters • An action filter is an attribute that modifies the way in which the action is executed. • Action filters can be applied to individual actions or at the controller level to apply to all actions in the controller. • The ASP. NET MVC framework includes several built-in action filters: • Output. Cache • Handle. Error • Authorize • You also can create your own custom action filters.

Views • A view is responsible for the UI (user interface). • A view

Views • A view is responsible for the UI (user interface). • A view is a template which binds and displays HTML controls with data coming from the model. • In MVC, views contain a combination of C# and html (. cshtml) • The. cshtml file uses the Razor view engine templates to produce HTML • The return type of view is “View. Result” or “Action. Result”

Razor Templates • Razor View engine allows us to use Razor templates to produce

Razor Templates • Razor View engine allows us to use Razor templates to produce HTML

Layout with Razor • _Layout. cshtml defines what you want to appear on every

Layout with Razor • _Layout. cshtml defines what you want to appear on every page of your application. • It is where you have your html, head, and body tags and where you define your navigation links.

HTML Helpers • The purpose of an html helper is to make it easy

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

ADO. NET • The Microsoft data access framework • A set of libraries that

ADO. NET • The Microsoft data access framework • A set of libraries that supports interactions with many data sources • Can be used with nearly all major database implementations such as SQL Server, Oracle, MS Access, and ODBC • Uses the disconnected data access architecture • The Entity Framework is built on ADO. NET classes

Entity Framework (or ADO. NET Entity Framework) • Entity Framework is the Microsoft version

Entity Framework (or ADO. NET Entity Framework) • Entity Framework is the Microsoft version of Object-Relational Mapper (ORM) • ORMs map objects to relational tables • Using the Entity Framework allows us to: • build and maintain data access in a much shorter time • focus on solving business problems without worrying about the underlying data storage • Use Language Integrated Query (LINQ)