1 / 29

virtual techdays

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

Download Presentation

virtual techdays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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?

  4. ASP.Net MVC Request Processing Architecture Views View Engine Custom View Engine WebForm - .Aspx Render View

  5. 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

  6. Routing Setting up Routes • Routes are configured in Global.asax

  7. Routing System.Web.Routing.Route

  8. Routing Understanding the Routing • Three main elements • RouteBase • Route • RouteCollection [RouteTable.Routes]

  9. Routing Routing Process

  10. 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

  11. INDIA │ 22-24 November 2010 virtual techdays DEMO: Custom Route Sundararajan S│ Assoc. Tech Architect, Aditi

  12. 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

  13. INDIA │ 22-24 November 2010 virtual techdays DEMO: Custom RouteHandler Sundararajan S│ Assoc. Tech Architect, Aditi

  14. Controller Introduction • Any incoming request is handled by the Controller

  15. Controller System.Web.MVC.Controller • Implements IController • Abstracts the Execute Method • Following features • Action Methods • Action results • Filters Filter ActionMethod ActionResult

  16. 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

  17. 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

  18. 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

  19. 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

  20. Controller Custom ActionResult • Inherit from ActionResult Class • Override ExecuteResult (ControllerContext context)

  21. INDIA │ 22-24 November 2010 virtual techdays DEMO: Custom ActionResult Sundararajan S│ Assoc. Tech Architect, Aditi

  22. Controller Filters – Attach reusable behaviors • Attach extra behaviors to controllers and actions • .Net Attribute Based • Four types of filters

  23. 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

  24. INDIA │ 22-24 November 2010 virtual techdays DEMO: Custom Action Filter Sundararajan S│ Assoc. Tech Architect, Aditi

  25. 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

  26. Controller Asynchronous Requests Increment the outstanding operations Set the parameters for the Completed Method Mark that FetchScoreCompleted can be called

  27. INDIA │ 22-24 November 2010 virtual techdays DEMO: Asynchronous Controllers Sundararajan S│ Assoc. Tech Architect, Aditi

  28. 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

  29. THANKS│ 22-24 November 2010 virtual techdays sundararajans@hotmail.com │ http://sundars.net http://www.codeshelve.com http://tinyurl.com/codeshelve

More Related