SlideShare une entreprise Scribd logo
1  sur  45
MVC Internals &
Extensibility

  Eyal Vardi
  CEO E4D Solutions LTD
  Microsoft MVP Visual C#
  blog: www.eVardi.com
Agenda
           Routing

           Dependency Resolver

           Controller Extensibility

           Model Extensibility

           View Extensibility




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
MVC in ASP.NET




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
MVC Execution Process
                                     ASP.NET




                   IIS                Routing                                ASP.NET
                                                                             MVC 3.0




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
URL Routing
     URL to Page
     http://Domain/Path




     URL to Controller

     http://Domain/.../..




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Routes
           A route is a URL pattern that is mapped to a
            handler.
                  The handler can be a physical file, such as an .aspx file in a
                   Web Forms application.
                  A handler can also be a class that processes the request,
                   such as a controller in an MVC application.

            protected void Application_Start()                               Global.asax
            {
                RouteTable
                  .Routes
                  .MapRoute(
                     "Default",    // Route name
                     "{controller}/{action}/{id}", // URL template
                     new { controller="Calc", action="Add", id=UrlParameter.Optional }
                   );
            }

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Route lifecycle




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Routing Process


                 Route                    MvcRouteHandler                  MvcHandler        Controller




                                                                       IDependencyResolver




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
URL Patterns
                      Route definition                                      Example of matching URL
     {controller}/{action}/{id}                                   /Products/show/beverages

     {table}/Details.aspx                                         /Products/Details.aspx

     blog/{action}/{entry}                                        /blog/show/123

     {reporttype}/{year}/{month}/{day}                            /sales/2008/1/5

     {locale}/{action}                                            /US/show

     {language}-{country}/{action}                                /en-US/show




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
URL Patterns in MVC Applications

           {Controller} / {Action} / {id}

           Query / {queryname} / {*queryvalues}
                  “*” This is referred to as a catch-all parameter.


           /query/select/bikes/onsale
                  queryname = "select" , queryvalues = "bikes/onsale"




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Adding Constraints to Routes
           Constraints are defined by using regular
            expressions string or by using objects that
            implement the IRouteConstraint interface.
            protected void Application_Start()                         Global.asax
            {
                RouteTable
                  .Routes
                  .MapRoute(
                     "Blog",
                     "Posts/{postDate}",
                     new { controller="Blog",action="GetPosts" }
                   );
            }
                  /Posts/28-12-71 => BlogController,GetPosts( “28-12-1971” )
                  /Post/28        => BlogController,GetPosts( “28” )
                  /Post/test      => BlogController,GetPosts( “test” )


© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
IRouteConstraint
          protected void Application_Start()                               Global.asax
          {
              RouteTable
                .Routes
                .MapRoute(
                   "Default",    // Route name
                   "{controller}/{action}/{id}", // URL template
                   new { controller="Calc", action="Add", id=UrlParameter.Optional },
                   new { action = new MyRouteConstraint() }
                 );
          }
                                                     Custom Route Constraint




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Regular Expression Route
   Constraint
          protected void Application_Start()
          {
              RouteTable
                .Routes
                .MapRoute(
                   "Default",    // Route name
                   "{controller}/{action}/{id}", // URL template
                   new { controller="Calc", action="Add", id=UrlParameter.Optional },
                   new { action = @"d{2}-d{2}-d{4}" }
                 );
          }
                                                           Regular Expression




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Custom Route
            Constraint


© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Dependency Resolver
           Service locator for the framework.
           protected void Application_Start()
           {
               AreaRegistration.RegisterAllAreas();
               RegisterGlobalFilters(GlobalFilters.Filters);
               RegisterRoutes(RouteTable.Routes);

                  DependencyResolver
                       .SetResolver(new TraceDependencyResolver() );
           }




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Factories and Providers




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Dependency Resolver


© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Controller Extensibility




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Controller & Filters
           The Controller class implements each of the
            filter interfaces.
           You can implement any of the filters for a
            specific controller by overriding the
            controller's On<Filter> method.
                  OnAuthorization
                  OnActionExecuting
                  OnActionExecuted
                  OnResultExecuting
                  OnResultExecuted
                  OnException


© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Action Method                   Action Result




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
ASP.NET MVC Filters
           Filters are custom classes that provide both a
            declarative and programmatic means to add
            pre-action and post-action behavior to
            controller action methods.

                      [HandleError]
                      [Authorize]
                      public class CourseController : Controller
                      {
                         [HandleError]
                         [OutputCache]
                         [RequireHttps]
                         public ActionResult Net( string name )
                         {
                             ViewBag.Course = BL.GetCourse(name);
                             return View();
                         }
                      }
© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
IAuthorizationFilter
           Make security decisions about whether to
            execute an action method.
                  AuthorizeAttribute

                  RequireHttpsAttribute




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
IActionFilter Interface
           OnActionExecuting
                  Runs before the action method.

           OnActionExecuted
                  Runs after the action method




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
IResultFilter Interface
             OnResultExecuting
                  Runs before the ActionResult object is executed.

           OnResultExecuted
                  Runs after the result.
                  Can perform additional processing of the result.
                  The OutputCacheAttribute is one example of a result filter.




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
IExceptionFilter
           Execute if there is an unhandled exception
            thrown during the execution of the ASP.NET
            MVC pipeline.
                  Can be used for logging or displaying an error page.

                  HandleErrorAttribute is one example of an exception filter.




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Filter Order
           Filters run in the following order:
                  Authorization filters

                  Action filters

                  Response filters

                  Exception filters




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Controller Context
                        1                                                            6




                        2                               3                        4   5




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Filters


© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Custom Filter
           You can create a filter in the following ways:
                  Override one or more of the controller's On<Filter> methods.

                  Create an attribute class that derives from
                   ActionFilterAttribute or FilterAttribute.
                  Register a filter with the filter provider (the FilterProviders
                   class).

                  Register a global filter using the GlobalFilterCollection class.




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Custom Filter




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Filter Providers
           By default, ASP.NET MVC registers the
            following filter providers:
                  Filters for global filters.

                  FilterAttributeFilterProvider for filter attributes.

                  ControllerInstanceFilterProvider for controller instances.

           The GetFilters method returns all of the
            IFilterProvider instances in the service
            locator.



© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
The Filter Provider Interface




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Model Extensibility




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Model Binders
           Binders are like type converters, because they
            can convert HTTP requests into objects that
            are passed to an action method.




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Model Binder Types
           ByteArrayModelBinder

           LinqBinaryModelBinder

           FormCollectionModelBinder

           HttpPostedFileBaseModelBinder

           DefaultModelBinder




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
DefaultModelBinder Class
           Translate HTTP request
            information into complex
            models, including nested
            types and arrays.

                  It does this through a naming
                   convention, which is supported at
                   both the HTML generation (HTML
                   helpers) and the consumption side
                   (model binders).




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Custom Model Binders
           ASP.NET MVC allows us to override both the
            default model binder, as well as add custom
            model binders.
            ModelBinders.Binders.DefaultBinder = new CustomDefaultBinder();

            ModelBinders.Binders.Add(
                   typeof(DateTime),
                   new DateTimeModelBinder() );




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
ModelBinder Attribute
           Represents an attribute that is used to
            associate a model type to a model-builder
            type.

            public ActionResult Contact(
             [ModelBinder(typeof(ContactBinder))] Contact contact )
            {
                  ViewData["name"]    = contact.Name;
                  ViewData["email"]   = contact.Email;
                  ViewData["message"] = contact.Message;
                  ViewData["title"]   = "Succes!";

                     return View();
            }




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Custom Binder


© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
public ActionResult Contact(
             [ModelBinder(typeof(ContactBinder))] Contact contact )
            {
                  ViewData["name"]    = contact.Name;
                  ViewData["email"]   = contact.Email;
                  ViewData["message"] = contact.Message;
                  ViewData["title"]   = "Succes!";

                     return View();
            }




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
View Extensibility




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il

Contenu connexe

Tendances

JAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun Gupta
JAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun GuptaJAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun Gupta
JAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun GuptaJAX London
 
What's new in JMS 2.0 - OTN Bangalore 2013
What's new in JMS 2.0 - OTN Bangalore 2013What's new in JMS 2.0 - OTN Bangalore 2013
What's new in JMS 2.0 - OTN Bangalore 2013Jagadish Prasath
 
What's new in Java Message Service 2?
What's new in Java Message Service 2?What's new in Java Message Service 2?
What's new in Java Message Service 2?Sivakumar Thyagarajan
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling RewriterJustin Edelson
 

Tendances (7)

JAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun Gupta
JAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun GuptaJAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun Gupta
JAX-RS 2.0: New and Noteworthy in RESTful Web Services API - Arun Gupta
 
What's new in JMS 2.0 - OTN Bangalore 2013
What's new in JMS 2.0 - OTN Bangalore 2013What's new in JMS 2.0 - OTN Bangalore 2013
What's new in JMS 2.0 - OTN Bangalore 2013
 
What's new in Java Message Service 2?
What's new in Java Message Service 2?What's new in Java Message Service 2?
What's new in Java Message Service 2?
 
Angular Workshop_Sarajevo2
Angular Workshop_Sarajevo2Angular Workshop_Sarajevo2
Angular Workshop_Sarajevo2
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling Rewriter
 
FraSCAti with OSGi
FraSCAti with OSGiFraSCAti with OSGi
FraSCAti with OSGi
 
Java ee7 1hour
Java ee7 1hourJava ee7 1hour
Java ee7 1hour
 

Similaire à Asp.Net Mvc Internals &amp; Extensibility

Prism Navigation
Prism NavigationPrism Navigation
Prism NavigationEyal Vardi
 
AMD & Require.js
AMD & Require.jsAMD & Require.js
AMD & Require.jsEyal Vardi
 
Web api routing
Web api routingWeb api routing
Web api routingEyal Vardi
 
Mvc & java script
Mvc & java scriptMvc & java script
Mvc & java scriptEyal Vardi
 
Batch Applications for the Java Platform
Batch Applications for the Java PlatformBatch Applications for the Java Platform
Batch Applications for the Java PlatformSivakumar Thyagarajan
 
Wcf data services
Wcf data servicesWcf data services
Wcf data servicesEyal Vardi
 
112815 java ee8_davidd
112815 java ee8_davidd112815 java ee8_davidd
112815 java ee8_daviddTakashi Ito
 
"Quantum" Performance Effects
"Quantum" Performance Effects"Quantum" Performance Effects
"Quantum" Performance EffectsSergey Kuksenko
 
Networking and Data Access with Eqela
Networking and Data Access with EqelaNetworking and Data Access with Eqela
Networking and Data Access with Eqelajobandesther
 
Asp.Net Mvc
Asp.Net MvcAsp.Net Mvc
Asp.Net Mvcmicham
 
Java API for WebSocket 1.0: Java EE 7 and GlassFish
Java API for WebSocket 1.0: Java EE 7 and GlassFishJava API for WebSocket 1.0: Java EE 7 and GlassFish
Java API for WebSocket 1.0: Java EE 7 and GlassFishArun Gupta
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Phpfunkatron
 
Job Managment Portlet
Job Managment PortletJob Managment Portlet
Job Managment Portletriround
 
JavaOne San Francisco 2013 - Servlet 3.1 (JSR 340)
JavaOne San Francisco 2013 - Servlet 3.1 (JSR 340)JavaOne San Francisco 2013 - Servlet 3.1 (JSR 340)
JavaOne San Francisco 2013 - Servlet 3.1 (JSR 340)Shing Wai Chan
 
Asp.Net MVC Framework Design Pattern
Asp.Net MVC Framework Design PatternAsp.Net MVC Framework Design Pattern
Asp.Net MVC Framework Design Patternmaddinapudi
 
OTN Tour 2013: What's new in java EE 7
OTN Tour 2013: What's new in java EE 7OTN Tour 2013: What's new in java EE 7
OTN Tour 2013: What's new in java EE 7Bruno Borges
 

Similaire à Asp.Net Mvc Internals &amp; Extensibility (20)

Prism Navigation
Prism NavigationPrism Navigation
Prism Navigation
 
AMD & Require.js
AMD & Require.jsAMD & Require.js
AMD & Require.js
 
Web api routing
Web api routingWeb api routing
Web api routing
 
Models
ModelsModels
Models
 
Asp.NET MVC
Asp.NET MVCAsp.NET MVC
Asp.NET MVC
 
Mvc & java script
Mvc & java scriptMvc & java script
Mvc & java script
 
Batch Applications for the Java Platform
Batch Applications for the Java PlatformBatch Applications for the Java Platform
Batch Applications for the Java Platform
 
Wcf data services
Wcf data servicesWcf data services
Wcf data services
 
112815 java ee8_davidd
112815 java ee8_davidd112815 java ee8_davidd
112815 java ee8_davidd
 
"Quantum" Performance Effects
"Quantum" Performance Effects"Quantum" Performance Effects
"Quantum" Performance Effects
 
Networking and Data Access with Eqela
Networking and Data Access with EqelaNetworking and Data Access with Eqela
Networking and Data Access with Eqela
 
Servlet 3.0
Servlet 3.0Servlet 3.0
Servlet 3.0
 
Asp.Net Mvc
Asp.Net MvcAsp.Net Mvc
Asp.Net Mvc
 
Java API for WebSocket 1.0: Java EE 7 and GlassFish
Java API for WebSocket 1.0: Java EE 7 and GlassFishJava API for WebSocket 1.0: Java EE 7 and GlassFish
Java API for WebSocket 1.0: Java EE 7 and GlassFish
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Php
 
Job Managment Portlet
Job Managment PortletJob Managment Portlet
Job Managment Portlet
 
Views
ViewsViews
Views
 
JavaOne San Francisco 2013 - Servlet 3.1 (JSR 340)
JavaOne San Francisco 2013 - Servlet 3.1 (JSR 340)JavaOne San Francisco 2013 - Servlet 3.1 (JSR 340)
JavaOne San Francisco 2013 - Servlet 3.1 (JSR 340)
 
Asp.Net MVC Framework Design Pattern
Asp.Net MVC Framework Design PatternAsp.Net MVC Framework Design Pattern
Asp.Net MVC Framework Design Pattern
 
OTN Tour 2013: What's new in java EE 7
OTN Tour 2013: What's new in java EE 7OTN Tour 2013: What's new in java EE 7
OTN Tour 2013: What's new in java EE 7
 

Plus de Eyal Vardi

Smart Contract
Smart ContractSmart Contract
Smart ContractEyal Vardi
 
Rachel's grandmother's recipes
Rachel's grandmother's recipesRachel's grandmother's recipes
Rachel's grandmother's recipesEyal Vardi
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2Performance Optimization In Angular 2
Performance Optimization In Angular 2Eyal Vardi
 
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)Eyal Vardi
 
Angular 2 NgModule
Angular 2 NgModuleAngular 2 NgModule
Angular 2 NgModuleEyal Vardi
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xUpgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xEyal Vardi
 
Angular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time CompilationAngular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time CompilationEyal Vardi
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And NavigationEyal Vardi
 
Angular 2 Architecture
Angular 2 ArchitectureAngular 2 Architecture
Angular 2 ArchitectureEyal Vardi
 
Angular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.xAngular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.xEyal Vardi
 
Angular 2.0 Views
Angular 2.0 ViewsAngular 2.0 Views
Angular 2.0 ViewsEyal Vardi
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Eyal Vardi
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0Template syntax in Angular 2.0
Template syntax in Angular 2.0Eyal Vardi
 
Http Communication in Angular 2.0
Http Communication in Angular 2.0Http Communication in Angular 2.0
Http Communication in Angular 2.0Eyal Vardi
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injectionAngular 2.0 Dependency injection
Angular 2.0 Dependency injectionEyal Vardi
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationEyal Vardi
 
Async & Parallel in JavaScript
Async & Parallel in JavaScriptAsync & Parallel in JavaScript
Async & Parallel in JavaScriptEyal Vardi
 
Angular 2.0 Pipes
Angular 2.0 PipesAngular 2.0 Pipes
Angular 2.0 PipesEyal Vardi
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 formsEyal Vardi
 

Plus de Eyal Vardi (20)

Why magic
Why magicWhy magic
Why magic
 
Smart Contract
Smart ContractSmart Contract
Smart Contract
 
Rachel's grandmother's recipes
Rachel's grandmother's recipesRachel's grandmother's recipes
Rachel's grandmother's recipes
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2Performance Optimization In Angular 2
Performance Optimization In Angular 2
 
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)
 
Angular 2 NgModule
Angular 2 NgModuleAngular 2 NgModule
Angular 2 NgModule
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xUpgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.x
 
Angular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time CompilationAngular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time Compilation
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And Navigation
 
Angular 2 Architecture
Angular 2 ArchitectureAngular 2 Architecture
Angular 2 Architecture
 
Angular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.xAngular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.x
 
Angular 2.0 Views
Angular 2.0 ViewsAngular 2.0 Views
Angular 2.0 Views
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0Template syntax in Angular 2.0
Template syntax in Angular 2.0
 
Http Communication in Angular 2.0
Http Communication in Angular 2.0Http Communication in Angular 2.0
Http Communication in Angular 2.0
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injectionAngular 2.0 Dependency injection
Angular 2.0 Dependency injection
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and Navigation
 
Async & Parallel in JavaScript
Async & Parallel in JavaScriptAsync & Parallel in JavaScript
Async & Parallel in JavaScript
 
Angular 2.0 Pipes
Angular 2.0 PipesAngular 2.0 Pipes
Angular 2.0 Pipes
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 

Asp.Net Mvc Internals &amp; Extensibility

  • 1. MVC Internals & Extensibility Eyal Vardi CEO E4D Solutions LTD Microsoft MVP Visual C# blog: www.eVardi.com
  • 2. Agenda  Routing  Dependency Resolver  Controller Extensibility  Model Extensibility  View Extensibility © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 3. MVC in ASP.NET © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 4. MVC Execution Process ASP.NET IIS Routing ASP.NET MVC 3.0 © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 5. URL Routing URL to Page http://Domain/Path URL to Controller http://Domain/.../.. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 6. Routes  A route is a URL pattern that is mapped to a handler.  The handler can be a physical file, such as an .aspx file in a Web Forms application.  A handler can also be a class that processes the request, such as a controller in an MVC application. protected void Application_Start() Global.asax { RouteTable .Routes .MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL template new { controller="Calc", action="Add", id=UrlParameter.Optional } ); } © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 7. Route lifecycle © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 8. Routing Process Route MvcRouteHandler MvcHandler Controller IDependencyResolver © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 9. URL Patterns Route definition Example of matching URL {controller}/{action}/{id} /Products/show/beverages {table}/Details.aspx /Products/Details.aspx blog/{action}/{entry} /blog/show/123 {reporttype}/{year}/{month}/{day} /sales/2008/1/5 {locale}/{action} /US/show {language}-{country}/{action} /en-US/show © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 10. URL Patterns in MVC Applications  {Controller} / {Action} / {id}  Query / {queryname} / {*queryvalues}  “*” This is referred to as a catch-all parameter.  /query/select/bikes/onsale  queryname = "select" , queryvalues = "bikes/onsale" © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 11. Adding Constraints to Routes  Constraints are defined by using regular expressions string or by using objects that implement the IRouteConstraint interface. protected void Application_Start() Global.asax { RouteTable .Routes .MapRoute( "Blog", "Posts/{postDate}", new { controller="Blog",action="GetPosts" } ); } /Posts/28-12-71 => BlogController,GetPosts( “28-12-1971” ) /Post/28 => BlogController,GetPosts( “28” ) /Post/test => BlogController,GetPosts( “test” ) © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 12. IRouteConstraint protected void Application_Start() Global.asax { RouteTable .Routes .MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL template new { controller="Calc", action="Add", id=UrlParameter.Optional }, new { action = new MyRouteConstraint() } ); } Custom Route Constraint © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 13. Regular Expression Route Constraint protected void Application_Start() { RouteTable .Routes .MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL template new { controller="Calc", action="Add", id=UrlParameter.Optional }, new { action = @"d{2}-d{2}-d{4}" } ); } Regular Expression © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 14. Custom Route Constraint © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 15. Dependency Resolver  Service locator for the framework. protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); DependencyResolver .SetResolver(new TraceDependencyResolver() ); } © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 16. Factories and Providers © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 17. Dependency Resolver © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 18. Controller Extensibility © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 19. Controller & Filters  The Controller class implements each of the filter interfaces.  You can implement any of the filters for a specific controller by overriding the controller's On<Filter> method.  OnAuthorization  OnActionExecuting  OnActionExecuted  OnResultExecuting  OnResultExecuted  OnException © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 20. Action Method Action Result © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 21. ASP.NET MVC Filters  Filters are custom classes that provide both a declarative and programmatic means to add pre-action and post-action behavior to controller action methods. [HandleError] [Authorize] public class CourseController : Controller { [HandleError] [OutputCache] [RequireHttps] public ActionResult Net( string name ) { ViewBag.Course = BL.GetCourse(name); return View(); } } © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 22. IAuthorizationFilter  Make security decisions about whether to execute an action method.  AuthorizeAttribute  RequireHttpsAttribute © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 23. IActionFilter Interface  OnActionExecuting  Runs before the action method.  OnActionExecuted  Runs after the action method © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 24. IResultFilter Interface  OnResultExecuting  Runs before the ActionResult object is executed.  OnResultExecuted  Runs after the result.  Can perform additional processing of the result.  The OutputCacheAttribute is one example of a result filter. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 25. IExceptionFilter  Execute if there is an unhandled exception thrown during the execution of the ASP.NET MVC pipeline.  Can be used for logging or displaying an error page.  HandleErrorAttribute is one example of an exception filter. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 26. Filter Order  Filters run in the following order:  Authorization filters  Action filters  Response filters  Exception filters © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 27. Controller Context 1 6 2 3 4 5 © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 28. Filters © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 29. Custom Filter  You can create a filter in the following ways:  Override one or more of the controller's On<Filter> methods.  Create an attribute class that derives from ActionFilterAttribute or FilterAttribute.  Register a filter with the filter provider (the FilterProviders class).  Register a global filter using the GlobalFilterCollection class. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 30. Custom Filter © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 31. Filter Providers  By default, ASP.NET MVC registers the following filter providers:  Filters for global filters.  FilterAttributeFilterProvider for filter attributes.  ControllerInstanceFilterProvider for controller instances.  The GetFilters method returns all of the IFilterProvider instances in the service locator. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 32. The Filter Provider Interface © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 33. Model Extensibility © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 34. Model Binders  Binders are like type converters, because they can convert HTTP requests into objects that are passed to an action method. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 35. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 36. Model Binder Types  ByteArrayModelBinder  LinqBinaryModelBinder  FormCollectionModelBinder  HttpPostedFileBaseModelBinder  DefaultModelBinder © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 37. DefaultModelBinder Class  Translate HTTP request information into complex models, including nested types and arrays.  It does this through a naming convention, which is supported at both the HTML generation (HTML helpers) and the consumption side (model binders). © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 38. Custom Model Binders  ASP.NET MVC allows us to override both the default model binder, as well as add custom model binders. ModelBinders.Binders.DefaultBinder = new CustomDefaultBinder(); ModelBinders.Binders.Add( typeof(DateTime), new DateTimeModelBinder() ); © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 39. ModelBinder Attribute  Represents an attribute that is used to associate a model type to a model-builder type. public ActionResult Contact( [ModelBinder(typeof(ContactBinder))] Contact contact ) { ViewData["name"] = contact.Name; ViewData["email"] = contact.Email; ViewData["message"] = contact.Message; ViewData["title"] = "Succes!"; return View(); } © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 40. Custom Binder © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 41. public ActionResult Contact( [ModelBinder(typeof(ContactBinder))] Contact contact ) { ViewData["name"] = contact.Name; ViewData["email"] = contact.Email; ViewData["message"] = contact.Message; ViewData["title"] = "Succes!"; return View(); } © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 42. View Extensibility © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 43. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 44. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 45. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il

Notes de l'éditeur

  1. http://mgolchin.net/posts/21/dive-deep-into-mvc-modelmetadata-and-modelmetadataprovider
  2. http://mgolchin.net/posts/20/dive-deep-into-mvc-imodelbinder-part-1