Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate...
-
Upload
charity-willis -
Category
Documents
-
view
217 -
download
3
Transcript of Virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate...
virtual techdaysINDIA │ 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
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
ASP.Net MVC Request Processing Architecture
View Engine
WebForm - .Aspx
Render View
Custom View Engine
Views
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
Routes are configured in Global.asax
Routing
Setting up Routes
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.
Three main elements RouteBase Route RouteCollection [RouteTable.Routes]
Routing
Understanding the Routing
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
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
virtual techdaysINDIA │ 22-24 November 2010
DEMO: Custom Route
Sundararajan S│ Assoc. Tech Architect, Aditi
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
virtual techdaysINDIA │ 22-24 November 2010
DEMO: Custom RouteHandler
Sundararajan S│ Assoc. Tech Architect, Aditi
Any incoming request is handled by the Controller
Controller
Introduction
IController
Custom ControllerSystem.Web.MVC.
Controller
HomeController (app specific controllers)
AccountController(app specific controllers)
Implements IController Abstracts the Execute Method Following features
Action Methods Action results Filters
Controller
System.Web.MVC.Controller
Filter
ActionMethod
ActionResult
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
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
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
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
Inherit from ActionResult Class Override ExecuteResult (ControllerContext context)
Controller
Custom ActionResult
virtual techdaysINDIA │ 22-24 November 2010
DEMO: Custom ActionResult
Sundararajan S│ Assoc. Tech Architect, Aditi
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
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
virtual techdaysINDIA │ 22-24 November 2010
DEMO: Custom Action Filter
Sundararajan S│ Assoc. Tech Architect, Aditi
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
Controller
Asynchronous Requests
Increment the outstanding operations
Mark that FetchScoreCompleted can be called
Set the parameters for the Completed Method
virtual techdaysINDIA │ 22-24 November 2010
DEMO: Asynchronous Controllers
Sundararajan S│ Assoc. Tech Architect, Aditi
virtual techdaysINDIA │ 22-24 November 2010
RESOURCES
Asp.Net MVC http://www.asp.net/mvc
Apress – Pro Asp.Net MVC2 Framework by Steven Sanderson
virtual techdaysTHANKS│ 22-24 November 2010
[email protected] │ http://sundars.nethttp://www.codeshelve.com http://tinyurl.com/codeshelve