Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate...

29
virtual techdays INDIA 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S Associate Tech Architect, Aditi Technologies

Transcript of Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate...

Page 1: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

virtual techdaysINDIA │ 22-24 November 2010

ASP.Net MVC Deep Dive

Sundararajan S │ Associate Tech Architect, Aditi Technologies

Page 2: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Asp.Net MVC Request Processing Routing

Introduction Understanding the Routing process Writing your custom Route Writing your Custom RouteHandler

Controller Action Methods Action Results Writing your own Custom ActionResult Filters CustomFilters Asynchronous Controllers

virtual techdaysINDIA │ 22-24 November 2010

S E S S I O N A G E N D A

Page 3: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

ASP.Net MVC Request Processing Architecture

Incoming HTTP Request

IIS / ASP.Net

URL Routing ModuleServe the file directly

Matching file on disk

RouteTable.RoutesIRouteHandler

IHttpHandler

MVCRouteHandler

ControllerFactoryIController

Custom controller factory

ActionMethodWrites directly to http response

Return ActionResult

Is A ViewResult?

Not A ViewResult?

ActionResult.Execute

Routing

Controller/ Actions

Page 4: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

ASP.Net MVC Request Processing Architecture

View Engine

WebForm - .Aspx

Render View

Custom View Engine

Views

Page 5: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

URLs are not expected to correspond to a Physical File Routes are configured

Maps each incoming URL to appropriate Request handler Class Constructs outgoing URLs

Routing Module is shared between both Asp.Net MVC and Asp.Net web forms System.web.Routing dll in Asp.Net 3.5 System.Web.dll in Asp.Net 4.0

Routing

Introduction

Page 6: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Routes are configured in Global.asax

Routing

Setting up Routes

Page 7: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Routing

System.Web.Routing.Route

Property Meaning Type Example

Url URL to be matched String “Blog/{blogID}”

RouteHandler Existing or custom route handler

IRouteHandler

new MVCRouteHandler()

Default Default Value for the parameter / Optional Parameters

RouteValueDictionary

Constraints Specify constraints on the Route parameters

RouteValueDictionary

DataTokens Other Route values that can be accessed during Routing

RouteValueDictionary

Areas.

Page 8: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Three main elements RouteBase Route RouteCollection [RouteTable.Routes]

Routing

Understanding the Routing

Page 9: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Routing

Routing Process

Url requested by the end user

Registered iHttpModules get invoked.UrlHttpModule gets invoked

First RouteBase object that matches this request in RouteTable.Routes is identified

Fetches the RouteData data Structure from the Matching Route [route , routehandler, values, datatokens]

Invoke RouteData ‘s RouteHandler

Page 10: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Create a new class that derives from RouteBase Implement GetRouteData(HttpContextBase httpContext)

Framework calls this method on each routetable data entry until one of them returns non null value

Return a routeData structure describing the chosen IRouteHandler Implement GetVirtualPath(RequestContext context, RouteValueDictionary

values) Outbound URL generation Framework calls this method on each routetable data entry until one of them returns non

null value

Routing

Custom Route

Page 11: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

virtual techdaysINDIA │ 22-24 November 2010

DEMO: Custom Route

Sundararajan S│ Assoc. Tech Architect, Aditi

Page 12: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Create new HttpHandler by implementing the interface IHttpHandler Implement the process request method of IHttpHandler Implement the interface IRouteHandler Implement GetHttpHandler(RequestContext requestContext)

Return the HttpHandler Created in the first step

Routing

Custom RouteHandler

Page 13: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

virtual techdaysINDIA │ 22-24 November 2010

DEMO: Custom RouteHandler

Sundararajan S│ Assoc. Tech Architect, Aditi

Page 14: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Any incoming request is handled by the Controller

Controller

Introduction

IController

Custom ControllerSystem.Web.MVC.

Controller

HomeController (app specific controllers)

AccountController(app specific controllers)

Page 15: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Implements IController Abstracts the Execute Method Following features

Action Methods Action results Filters

Controller

System.Web.MVC.Controller

Filter

ActionMethod

ActionResult

Page 16: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Three ways to read the input in the Action method Read Values from context objects Parameters to Action methods Model Binding

Commonly used Context objects Request - .QueryString, .Form, .Cookies, .HttpMethod, .Headers, .Url, .UserHostAddress RouteData - .Route, .Values User TempData HttpContext -.Application, .Cache, .Items, .Session

Controller

Reading Input

Page 17: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Parameter objects are instantiated using Value Providers and Model Binders

Value type parameters are inherently compulsory To make them optional – 1. specify a default value (or) 2. make them nullable (double?)

Reference Parameters are inherently optional To make them compulsory, check for the null value and throw exception using custom

code. Default Values for parameters

.Net 3.5 – Mark the parameter with the attribute [DefaultValue()] .Net 4.0 – Use optional parameter syntax

ActionMethods cannot have out/ref parameters

Controller

ActionMethod Parameters

Page 18: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Main types of responses Retun HTML by rendering a view

ViewResult , PartialViewresult

Redirect page – HTTP Redirection RedirectToRouteResult, RedirectResult

Other data to the responses output stream ContentResult, JsonResult, JavascriptResult, FileResult, HTTPUnAuthorizedresult, EmptyResult

All Action Results derive from Action Result All Action results have a method – ExecuteResult() This is an example of command pattern

Controller

ActionResult

Page 19: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

ViewData Dictionary KeyValue pair To pass data from the actionmethod to the view

Strongly type Model ViewData.Model Create a strongly type view page

Dynamic object in .Net 4.0 ViewPage<dynamic>

TempData[“key”] to share data across redirections

Controller

Sharing Data

Page 20: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Inherit from ActionResult Class Override ExecuteResult (ControllerContext context)

Controller

Custom ActionResult

Page 21: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

virtual techdaysINDIA │ 22-24 November 2010

DEMO: Custom ActionResult

Sundararajan S│ Assoc. Tech Architect, Aditi

Page 22: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Attach extra behaviors to controllers and actions .Net Attribute Based Four types of filters

Controller

Filters – Attach reusable behaviors

Filter Type Interface When Run Default Implementation

Authorizationfilter IAuthorizationFilter First AuthorizeAttribute

Action Filter IActionFilter Before and after action method

ActionFilterAttribute

ResultFilter IResultFilter Before and after action result is executed

ActionFilterAttribute

Exception filter IExceptionFilter Unhandled Exception HandleErrorAttribute

Page 23: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

IActionFilter OnActionExecuting() – Before that Action method runs OnActionExecuted() – After the Action method Runs

IResultFilter OnResultExecuting() –Before the actionResult is executed OnResultExecuted() – After the actionResult is executed

Controller

Filters – Custom Action and Result Filters

Page 24: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

virtual techdaysINDIA │ 22-24 November 2010

DEMO: Custom Action Filter

Sundararajan S│ Assoc. Tech Architect, Aditi

Page 25: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Three ways to build Asynchrony in Asp.Net MVC RouteHandler ‘s GetHttpHandler() returns a type of IHttpAsyncHandler

Works directly with underlying core platform Bypasses ASP.Net MVC

Create a custom Controller type , that implements IAsyncController Inherit your controller from AsyncController

Inherit from AsyncController Create two methods for every action 1. <ActionMethodName>Async 2. <ActionMethodName>Completed

Controller

Asynchronous Requests

Page 26: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

Controller

Asynchronous Requests

Increment the outstanding operations

Mark that FetchScoreCompleted can be called

Set the parameters for the Completed Method

Page 27: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

virtual techdaysINDIA │ 22-24 November 2010

DEMO: Asynchronous Controllers

Sundararajan S│ Assoc. Tech Architect, Aditi

Page 28: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

virtual techdaysINDIA │ 22-24 November 2010

RESOURCES

Asp.Net MVC http://www.asp.net/mvc

Apress – Pro Asp.Net MVC2 Framework by Steven Sanderson

Page 29: Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies.

virtual techdaysTHANKS│ 22-24 November 2010

[email protected] │ http://sundars.nethttp://www.codeshelve.com http://tinyurl.com/codeshelve