Download presentation
Presentation is loading. Please wait.
Published byCharity Willis Modified over 9 years ago
1
virtual techdays INDIA │ 22-24 November 2010 ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies
2
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 techdays INDIA │ 22-24 November 2010 S E S S I O N A G E N D A
3
ASP.Net MVC Request Processing Architecture Incoming HTTP Request IIS / ASP.Net URL Routing Module Serve the file directly Matching file on disk RouteTable.Routes IRouteHandler IHttpHandler MVCRouteHandler ControllerFactory IController Custom controller factory ActionMethod Writes directly to http response Return ActionResult Is A ViewResult? Not A ViewResult? ActionResult.Execute Routing Controller/ Actions
4
ASP.Net MVC Request Processing Architecture View Engine WebForm -.Aspx Render View Custom View Engine Views
5
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
6
Routes are configured in Global.asax Routing Setting up Routes
7
Routing System.Web.Routing.Route PropertyMeaningTypeExample UrlURL to be matchedString“Blog/{blogID}” RouteHandlerExisting or custom route handler IRouteHandle r new MVCRouteHandler() DefaultDefault Value for the parameter / Optional Parameters RouteValueDi ctionary ConstraintsSpecify constraints on the Route parameters RouteValueDi ctionary DataTokensOther Route values that can be accessed during Routing RouteValueDi ctionary Areas.
8
Three main elements RouteBase Route RouteCollection [RouteTable.Routes] Routing Understanding the Routing
9
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
10
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
11
virtual techdays INDIA │ 22-24 November 2010 DEMO: Custom Route Sundararajan S │ Assoc. Tech Architect, Aditi
12
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
13
virtual techdays INDIA │ 22-24 November 2010 DEMO: Custom RouteHandler Sundararajan S │ Assoc. Tech Architect, Aditi
14
Any incoming request is handled by the Controller Controller Introduction IControllerCustom Controller System.Web.MVC. Controller HomeController (app specific controllers) AccountController(app specific controllers)
15
Implements IController Abstracts the Execute Method Following features Action Methods Action results Filters Controller System.Web.MVC.Controller FilterActionMethod Action Result
16
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
17
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
18
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
19
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 TempData[“key”] to share data across redirections Controller Sharing Data
20
Inherit from ActionResult Class Override ExecuteResult (ControllerContext context) Controller Custom ActionResult
21
virtual techdays INDIA │ 22-24 November 2010 DEMO: Custom ActionResult Sundararajan S │ Assoc. Tech Architect, Aditi
22
Attach extra behaviors to controllers and actions .Net Attribute Based Four types of filters Controller Filters – Attach reusable behaviors Filter TypeInterfaceWhen RunDefault Implementation AuthorizationfilterIAuthorizationFilterFirstAuthorizeAttribute Action FilterIActionFilterBefore and after action method ActionFilterAttribute ResultFilterIResultFilterBefore and after action result is executed ActionFilterAttribute Exception filterIExceptionFilterUnhandled ExceptionHandleErrorAttribute
23
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
24
virtual techdays INDIA │ 22-24 November 2010 DEMO: Custom Action Filter Sundararajan S │ Assoc. Tech Architect, Aditi
25
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. Async 2. Completed Controller Asynchronous Requests
26
Controller Asynchronous Requests Increment the outstanding operations Mark that FetchScoreCompleted can be called Set the parameters for the Completed Method
27
virtual techdays INDIA │ 22-24 November 2010 DEMO: Asynchronous Controllers Sundararajan S │ Assoc. Tech Architect, Aditi
28
virtual techdays INDIA │ 22-24 November 2010 RESOURCES Asp.Net MVC http://www.asp.net/mvc Apress – Pro Asp.Net MVC2 Framework by Steven Sanderson
29
virtual techdays THANKS │ 22-24 November 2010 sundararajans@hotmail.com │ http://sundars.nethttp://sundars.net http://www.codeshelve.com http://tinyurl.com/codeshelve
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.