overview of mvc framework - by software outsourcing company india

29
Prepared By: Mr. Dhrumil Patel iFour Consultancy MVC

Upload: jignesh-aakoliya

Post on 14-Apr-2017

65 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Overview of MVC Framework - by software outsourcing company india

Prepared By:Mr. Dhrumil Patel

iFour Consultancy

MVC

Page 2: Overview of MVC Framework - by software outsourcing company india

Introduction to MVC

MVC is a software design pattern for developing web applications. It is a software architecture pattern which separates the representation of

information from the users interaction with it. Here M stands for Model, V stands for View and C stands for controller. ASP.NET MVC helps us to develop powerful and pattern-based dynamic websites

that enables a clean separation of concerns and also gives you a full control on a mark-up.

Also it includes many features that help us to develop a sophisticated and modern web application.

#C Company Indiahttp://www.ifourtechnolab.com

Page 3: Overview of MVC Framework - by software outsourcing company india

MVC vs WEB FORMS

MVC Web FormsEasier to Manage Complexity. Preservers State over HTTP.

Does not use view state or server based forms. Page Controller Pattern.

Rich Routing Structure. View state or server based forms.

Support for Test-Driven Development. Works well for small teams.

Supports Large Teams Well. Development is less complex.

#C Company Indiahttp://www.ifourtechnolab.com

Page 4: Overview of MVC Framework - by software outsourcing company india

Architecture of MVC

A Model View Controller pattern is made up of the following three parts:ModelViewController

•A markup language is a set of markup tags

#C Company Indiahttp://www.ifourtechnolab.com

Page 5: Overview of MVC Framework - by software outsourcing company india

Architecture of MVC (Cont.)

Model:The model is responsible for managing the data of the application. It responds to the request from the view and it also responds to instructions from the

controller to update itself. It is the lowest level of the pattern which is responsible for maintaining data. The Model represents the application core (for instance a list of database records). It is also called the domain layer

•A markup language is a set of markup tags

#C Company Indiahttp://www.ifourtechnolab.com

Page 6: Overview of MVC Framework - by software outsourcing company india

Architecture of MVC (Cont.)

View:The View displays the data (the database records). A view requests information from the model, that it needs to generate an output

representation. It presents data in a particular format like JSP, ASP, PHP. MVC is often seen in web applications, where the view is the HTML page.

•A markup language is a set of markup tags

#C Company Indiahttp://www.ifourtechnolab.com

Page 7: Overview of MVC Framework - by software outsourcing company india

Architecture of MVC (Cont.)

Controller:The Controller is the part of the application that handles user interaction.Typically controllers read data from a view, control user input, and send input data to

the model. It handles the input, typically user actions and may invoke changes on the model and

view.

•A markup language is a set of markup tags

#C Company Indiahttp://www.ifourtechnolab.com

Page 8: Overview of MVC Framework - by software outsourcing company india

Routing

A route is a URL pattern that is mapped to a handler. The handler can be a physical file, such as an .aspx file in a Web Forms application. A handler can also be a class that processes the request, such as a controller in an MVC application.

At runtime, Routing engine use the Route table for matching the incoming request's URL pattern against the URL patterns defined in the Route table. You can register one or more URL patterns to the Route table at Application_Start event.

When the routing engine finds a match in the route table for the incoming request's URL, it forwards the request to the appropriate controller and action. If there is no match in the route table for the incoming request's URL, it returns a 404 HTTP status code.

•A markup language is a set of markup tags

#C Company Indiahttp://www.ifourtechnolab.com

Page 9: Overview of MVC Framework - by software outsourcing company india

How it works?•A markup language is a set of markup tags

#C Company Indiahttp://www.ifourtechnolab.com

Page 10: Overview of MVC Framework - by software outsourcing company india

How to define route?

public static void RegisterRoutes(RouteCollection routes){ routes.MapRoute( "Default", //Route name "{controller}/{action}/{id}", //URL with parameters

new { controller = "Home", action = "Index", id = UrlParameter.Optional } //Default parameters );}protected void Application_Start(){

RegisterRoutes(RouteTable.Routes);//To:DO

}

•A markup language is a set of markup tags

#C Company Indiahttp://www.ifourtechnolab.com

Page 11: Overview of MVC Framework - by software outsourcing company india

Actions are the ultimate request destinationPublic controller methodsNon-staticNo return value restrictions

Actions typically return an ActionResult.

Actions

#C Company Indiahttp://www.ifourtechnolab.com

Page 12: Overview of MVC Framework - by software outsourcing company india

Controller action response to a browser requestInherits from the base ActionResult classDifferent results types:

Action Result

#C Company Indiahttp://www.ifourtechnolab.com

Page 13: Overview of MVC Framework - by software outsourcing company india

Action Result (Cont.)

#C Company Indiahttp://www.ifourtechnolab.com

Page 14: Overview of MVC Framework - by software outsourcing company india

ASP.NET MVC maps the data from the HTTP request to action parameters in few ways:Routing engine can pass parameters to actions

http://localhost/Users/IFourRouting pattern: Users/{username}

URL query string can contains parameters/Users/ByUsername?username=IFour

HTTP post data can also contain parameters

Action Parameters

#C Company Indiahttp://www.ifourtechnolab.com

Page 15: Overview of MVC Framework - by software outsourcing company india

ActionName(string name) AcceptVerbs

HttpPostHttpGetHttpDeleteHttpOptions

NonAction RequireHttps ChildActionOnly – Only for Html.Action()

Action Selection

#C Company Indiahttp://www.ifourtechnolab.com

Page 16: Overview of MVC Framework - by software outsourcing company india

Action Filters

ASP.NET MVC provides the following types of action filters:1. Authorization filter, which makes security decisions about whether to execute an action method, such

as performing authentication or validating properties of the request. The AuthorizeAttribute class is one example of an authorization filter.

2. Action filter, which wraps the action method execution. This filter can perform additional processing, such as providing extra data to the action method, inspecting the return value, or canceling execution of the action method.

3. Result filter, which wraps execution of the ActionResult object. This filter can perform additional processing of the result, such as modifying the HTTP response. The OutputCacheAttribute class is one example of a result filter.

4. Exception filter, which executes if there is an unhandled exception thrown somewhere in action method, starting with the authorization filters and ending with the execution of the result. Exception filters can be used for tasks such as logging or displaying an error page. The HandleErrorAttribute class is one example of an exception filter.

•A markup language is a set of markup tags

#C Company Indiahttp://www.ifourtechnolab.com

Page 17: Overview of MVC Framework - by software outsourcing company india

How To Apply an Action Filter?

Typically, an action filter is an attribute that implements the abstract FilterAttribute class. Some action filters, such as AuthorizeAttribute and HanleErrorAttribute, implement the FilterAttribute class directly. These action filters are always called before the action method runs.

Other action filters, such as OutputCacheAttribute , implement the abstract ActionFilterAttribute class, which enables the action filter to run either before or after the action method runs.

You can use the action filter attribute to mark any action method or controller. If the attribute marks a controller, the action filter applies to all action methods in that controller.

The following example shows the default implementation of the HomeController class. In the example, the HandleError attribute is used to mark the controller. Therefore, the filter applies to both action methods in the controller.

•A markup language is a set of markup tags

#C Company Indiahttp://www.ifourtechnolab.com

Page 18: Overview of MVC Framework - by software outsourcing company india

Example for Action Filter

[HandleError]public class HomeController : Controller{ public ActionResult Index() { ViewData["Message"] = "Welcome to ASP.NET MVC!"; return View(); }

OutputCache(Duration=10)] public ActionResult About() { return View(); }}

•A markup language is a set of markup tags

#C Company Indiahttp://www.ifourtechnolab.com

Page 19: Overview of MVC Framework - by software outsourcing company india

Template markup syntax Simple-syntax view engine Based on the C# programming language Enables the programmer to use an HTML construction workflow Code-focused templating approach, with minimal transition between HTML and

code:Razor syntax starts code blocks with a @ character and does not require explicit closing

of the code-block

Razor

#C Company Indiahttp://www.ifourtechnolab.com

Page 20: Overview of MVC Framework - by software outsourcing company india

@ – For values (HTML encoded)

@{ … } – For code blocks (keep the view simple!)

Razor Syntax

<p> Current time is: @DateTime.Now!!! Not HTML encoded value: @Html.Raw(someVar)</p>

@{ var productName = "Energy drink"; if (Model != null) { productName = Model.ProductName; } else if (ViewBag.ProductName != null) { productName = ViewBag.ProductName; }} <p>Product "@productName" has been added in your shopping cart</p>

#C Company Indiahttp://www.ifourtechnolab.com

Page 21: Overview of MVC Framework - by software outsourcing company india

If, else, for, foreach, etc. C# statementsHTML markup lines can be included at any part@: – For plain text line to be rendered

Razor Syntax (Cont.)

<div class="products-list">@if (Model.Products.Count() == 0){ <p>Sorry, no products found!</p>}else{ @:List of the products found: foreach(var product in Model.Products) { <b>@product.Name, </b> }}</div>

#C Company Indiahttp://www.ifourtechnolab.com

Page 22: Overview of MVC Framework - by software outsourcing company india

Define a common site templateSimilar to ASP.NET master pages (but better!)Razor view engine renders content inside-out

First view is redered, then layout@RenderBody() –

indicate where we wantthe views based on thislayout to “fill in” theircore content at thatlocation in the HTML

Layout

#C Company Indiahttp://www.ifourtechnolab.com

Page 23: Overview of MVC Framework - by software outsourcing company india

Views don't need to specify layout since their default layout is set in their _ViewStart file:~/Views/_ViewStart.cshtml (code for all views)

Each view can specify custom layout pages.

Views without layout:

View and Layout

@{ Layout = "~/Views/Shared/_UncommonLayout.cshtml";}

@{ Layout = null;}

#C Company Indiahttp://www.ifourtechnolab.com

Page 24: Overview of MVC Framework - by software outsourcing company india

With MVC, HTML helpers are much like traditional ASP.NET Web Form controls. Just like web form controls in ASP.NET, HTML helpers are used to modify HTML. But HTML

helpers are more lightweight. Unlike Web Form controls, an HTML helper does not have an event model and a view state.

In most cases, an HTML helper is just a method that returns a string. With MVC, you can create your own helpers, or use the built in HTML helpers.

HTML Helpers in MVC

#C Company Indiahttp://www.ifourtechnolab.com

Page 25: Overview of MVC Framework - by software outsourcing company india

HTML Helpers in MVC (Cont.)

#C Company Indiahttp://www.ifourtechnolab.com

Page 26: Overview of MVC Framework - by software outsourcing company india

HTML Helpers in MVC (Cont.)

#C Company Indiahttp://www.ifourtechnolab.com

Page 27: Overview of MVC Framework - by software outsourcing company india

Partial views render portions of a pageReuse pieces of a viewHtml helpers – Partial, RenderPartial and Action

Razor partial views are still .cshtml files.

Partial View

Located in the same folder as other views or in Shared folder

#C Company Indiahttp://www.ifourtechnolab.com

Page 28: Overview of MVC Framework - by software outsourcing company india

Some applications can have a large number of controllers ASP.NET MVC lets us partition Web applications into smaller units (areas) An area is effectively an MVC structure inside an application Example: large e-commerce application

Main store, usersBlog, forumAdministration

Areas

#C Company Indiahttp://www.ifourtechnolab.com

Page 29: Overview of MVC Framework - by software outsourcing company india

Thank you

Software development company indiahttp://www.ifourtechnolab.com