290 likes | 503 Views
INDIA │ 22-24 November 2010. virtual techdays. ASP.Net MVC Deep Dive. Sundararajan S │ Associate Tech Architect, Aditi Technologies. INDIA │ 22-24 November 2010. virtual techdays. Asp.Net MVC Request Processing Routing Introduction Understanding the Routing process
E N D
INDIA │ 22-24 November 2010 virtual techdays ASP.Net MVC Deep Dive Sundararajan S │ Associate Tech Architect, Aditi Technologies
INDIA │ 22-24 November 2010 virtual techdays • 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 S E S S I O N A G E N D A
ASP.Net MVC Request Processing Architecture Incoming HTTP Request Routing IIS / ASP.Net Matching file on disk Serve the file directly URL Routing Module RouteTable.Routes IRouteHandler IHttpHandler MVCRouteHandler Controller/ Actions Custom controller factory IController ControllerFactory Writes directly to http response ActionMethod Not A ViewResult? Return ActionResult ActionResult.Execute Is A ViewResult?
ASP.Net MVC Request Processing Architecture Views View Engine Custom View Engine WebForm - .Aspx Render View
Routing Introduction • 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.Routingdll in Asp.Net 3.5 • System.Web.dll in Asp.Net 4.0
Routing Setting up Routes • Routes are configured in Global.asax
Routing System.Web.Routing.Route
Routing Understanding the Routing • Three main elements • RouteBase • Route • RouteCollection [RouteTable.Routes]
Routing Routing Process
Routing Custom Route • Create a new class that derives from RouteBase • Implement GetRouteData(HttpContextBasehttpContext) • 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
INDIA │ 22-24 November 2010 virtual techdays DEMO: Custom Route Sundararajan S│ Assoc. Tech Architect, Aditi
Routing Custom RouteHandler • Create new HttpHandler by implementing the interface IHttpHandler • Implement the process request method of IHttpHandler • Implement the interface IRouteHandler • Implement GetHttpHandler(RequestContextrequestContext) • Return the HttpHandler Created in the first step
INDIA │ 22-24 November 2010 virtual techdays DEMO: Custom RouteHandler Sundararajan S│ Assoc. Tech Architect, Aditi
Controller Introduction • Any incoming request is handled by the Controller
Controller System.Web.MVC.Controller • Implements IController • Abstracts the Execute Method • Following features • Action Methods • Action results • Filters Filter ActionMethod ActionResult
Controller Reading Input • 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 ActionMethod Parameters • 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 ActionResult • 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 Sharing Data • 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 Custom ActionResult • Inherit from ActionResult Class • Override ExecuteResult (ControllerContext context)
INDIA │ 22-24 November 2010 virtual techdays DEMO: Custom ActionResult Sundararajan S│ Assoc. Tech Architect, Aditi
Controller Filters – Attach reusable behaviors • Attach extra behaviors to controllers and actions • .Net Attribute Based • Four types of filters
Controller Filters – Custom Action and Result Filters • 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
INDIA │ 22-24 November 2010 virtual techdays DEMO: Custom Action Filter Sundararajan S│ Assoc. Tech Architect, Aditi
Controller Asynchronous Requests • 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 Increment the outstanding operations Set the parameters for the Completed Method Mark that FetchScoreCompleted can be called
INDIA │ 22-24 November 2010 virtual techdays DEMO: Asynchronous Controllers Sundararajan S│ Assoc. Tech Architect, Aditi
INDIA │ 22-24 November 2010 virtual techdays • Asp.Net MVC • http://www.asp.net/mvc • Apress – Pro Asp.Net MVC2 Framework by Steven Sanderson RESOURCES
THANKS│ 22-24 November 2010 virtual techdays sundararajans@hotmail.com │ http://sundars.net http://www.codeshelve.com http://tinyurl.com/codeshelve