SlideShare a Scribd company logo
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
ASP.NET vs. ASP.NET MVC




© 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
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
URL Routing Pipeline




© 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
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
Custom Route
            Constraint


© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Routing Summary
           URL Templates
                  Default values
                  Namespaces
                  Constraint

           IRouteHandler

           RouteBase




© 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()
           {
               ...

                  DependencyResolver.SetResolver(new TraceDependencyResolver() );
           }




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




© 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
Understanding Controllers
           MVC controllers are responsible for
            responding to requests made against an
            ASP.NET MVC website.




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




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Creation of the Controller
           IControllerFactory
            Responsible for determining the
            controller to service the request.
           IControllerActivator
            Responsible for creating the
            controller
           DefaultControllerFactory has
            internal class called
            DefaultControllerActivator.


© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Creation of the Controller
  Request
                  Routing                                   MvcRouteHandler                   MvcHandler




          Dependency              Controller               Dependency            Controller
           Resolver                                         Resolver                          Controller
                                   Factory                                       Activator




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

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

           IControllerFactory

           IControllerActivator




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
The Action Execution
  Request
                  Routing                                   MvcRouteHandler                    MvcHandler




          Dependency              Controller               Dependency             Controller
           Resolver                                         Resolver                           Controller
                                   Factory                                        Activator




                                                                                  Action        Action
                                                                                 Invoker        Method




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
The Action Execution
           Once the controller has been instantiated, the
            specified action has to be executed, and this
            is handled by the IActionInvoker.
                  If you derived your controller from the Controller class, then
                   this is the responsibility of an action invoker.




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


© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Built-In Action Invoker
           Finds a method that has the
            same name as the requested
            action.




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Custom Action Name
           You can then accept an action name that
            wouldn’t be legal as a C# method name.
                  [ActionName("User-Registration")]
                  [ActionNameSelector]




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Action Method Selection
           The action invoker uses an action method
            selector to remove ambiguity when selecting
            an action.
           The invoker gives preference to the actions
            that have selectors.




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Custom Action Method Selector
           You return true from IsValidForRequest if the
            method is able to process a request, and false
            otherwise.




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


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




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

           Action Name Selector

           Action Method Selector

           Handling Unknown Actions




© 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
What Is a Model?
                                                                                     Model Binding
                                                           Model

                                                                                     Deserialization



       HTML                                   View                      Controller            HTTP

            Template




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

        public ActionResult Index(Reservation reservation)
        {
             // TODO BL.
             return View( reservation );
        }                                            Deserialization




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




© 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
DefaultModelBinder Class
           Translate HTTP request into
            complex models, including
            nested types and arrays.
                  URL
                  Form data, Culture - Sensitive


            1.     Request.Form

            2.     RoutedData.Values
                                                                (Key,Value)
            3.     Request.QueryString

            4.     Request.Files


© 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
Binding to Complex Types
        [DisplayName("Book")]                                                    Model
        public class E4DBook
        {
               [Display(Name = "Title")]
                public string E4DTitle { get; set; }

                     public Reservation MyReservation { get; set; }
        }



        @Html.LabelFor(model => model.MyReservation.FlightNum)                   View
        @Html.EditorFor(model => model.MyReservation.FlightNum)


                                                                                 Html
       <label for="MyReservation_FlightNum">FlightNum</label>

       <input type="text" value=""
            id   = "MyReservation_FlightNum"
            name = "MyReservation.FlightNum"                         />

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Specifying Custom Prefixes
                                                                                      Controller
        public ActionResult Index(
                Reservation res1 ,
                [Bind(Prefix="myRes")] Reservation res2 ) { ... }




      <!– First Arg -->                                                                     Html
      <label for="FlightNum">FlightNum</label>
      <input type="text" id="FlightNum" name="FlightNum"                         />

      ...

      <!– Second Arg -->
      <label for="MyRes_FlightNum">FlightNum</label>
      <input type="text" id="MyRes_FlightNum" name="MyRes.FlightNum"                   />




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Bind Attribute (“View”)
           Represents an attribute that is used to provide
            details about how model binding to a
            parameter should occur.


            [Bind(Include = "FlightNum, TravelDate)]
            public class Reservation
            {
                public int FlightNum { get; set; }
                public DateTime TravelDate { get; set; }
                public int TicketCount { get; set; }
                public int DiscountPercent { get; set; }
            }




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


© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Binding to a String Collections
                                                                                 Controller
        public ActionResult Index( List< string > reservation ) { ... }




                                                                                    Html
      <input      type="text"        name="reservation"              />
      <input      type="text"        name="reservation"              />
      <input      type="text"        name="reservation"              />
      <input      type="text"        name="reservation"              />




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Binding to a Collections
                                                                                      Controller
        public ActionResult Index( List<Reservation> reservation ) { ... }




       <!– Option I-->                                                                   Html
       <input type="text" name="[0].Name"                       />
       <input type="text" name="[1].Name"                        />

       <!– Option II -->
       <input type="hidden" name="Index" value="f1"                              />
       <input type="text" name="[f1].Name" />

       <input type="hidden" name="Index" value="f2"                              />
       <input type="text" name="[f2].Name" />




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Binding to a Dictionary
                                                                                      Controller
        public ActionResult Index( Dictionary<Reservation> reservation ) { ... }




                                                                                         Html
       <input type="hidden" name="[0].key" value="f1"                            />
       <input type="text" name="[0].value.Name" />

       <input type="hidden" name="[1].key" value="f2"                            />
       <input type="text" name="[1].value.Name" />




© 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
© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Model Summary
           Http to .NET Classes
                  IModelBinder
                  IValueProvider
                  ModelMetadataProvider
                  BindAttribute

           Validation
                  Data Annotations
                  IValidateObject




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Action Results
           A controller action returns
            something called an action result.
           Action results types:
                  ViewResult                                   FileContentResult

                  EmptyResult                                  FilePathResult

                  RedirectResult                               FileStreamResult

                  JsonResult                                   HttpNotFoundResult

                  JavaScriptResult                             HttpRedirectResult

                  ContentResult                                HttpStatusCodeResult

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Custom 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
                      {
                         [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
Filter Interfaces

                                           Action Method                   Action Result




© 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 & 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
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
Handle Error Attribute
           You can specify an exception and the names
            of a view and layout.
           Works only when custom errors
            are enabled in the Web.config file
                  <customErrors mode="On" /> inside
                   the <system.web>


           The view get
            HandleErrorInfo




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
First                    Global                    Controller                 Action           Last
   Filter I                   Filter I                    Filter I                 Filter I       Filter I
   Filter II                  Filter II                   Filter II                Filter II      Filter II


                                          Action Method                   Action Result




      Authorization                          Action                         Result              Exception
       Filter I                        Filter I     Filter I          Filter I     Filter I    Filter II
        Filter I                                                                                Filter II
           Filter I                                                                                Filter II
             Filter I                  Filter I     Filter I          Filter I     Filter I          Filter II
               Filter I                                                                                Filter II
                                       Filter I     Filter I          Filter I     Filter I
      Filter II
       Filter II                       Filter I     Filter I          Filter I                 Filter I
          Filter II                                                                Filter I     Filter I
            Filter II                                                                              Filter I
              Filter II                Filter I     Filter I          Filter I     Filter I          Filter I
                                                                                                       Filter I
© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Action

                                    Before              After
                                   Filter I         Filter II
                                   Filter I         Filter II
                                   Filter I         Filter II
                                   Filter I         Filter II
                                   Filter I         Filter II
                                   Filter II        Filter I
                                   Filter II        Filter I
                                   Filter II        Filter I
                                   Filter II        Filter I
                                   Filter II        Filter I




© 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
The Filter Provider Interface




© 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
Filter Summary
           Custom Filters
                  IAuthorizationFilter
                  IActionFilter
                  IResultFilter
                  IExceptionFilter


           IFilterProvider




© 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
Creating a Custom View Engine




© 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
View Summary
           IViewEngine
                  How to find the view?

           IView
                  Render




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

                                       Mvc
           Routing                 RouteHandler
                                                           MvcHandler



          Dependency              Controller              Dependency             Controller
           Resolver                                        Resolver                              Controller
                                   Factory                                       Activator

             Action                 Name                    Method               Authorization    Model
            Invoker                Selector                Selectror                 Filter       Bind

            Action                  Action                   Action               Result           View
             Filer                  Method                    Filer               Filter          Engine

                                     Result               Exception
             View
                                     Filter                 Filter




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

More Related Content

Similar to Asp.net mvc internals & extensibility

Asp.Net Mvc Internals &amp; Extensibility
Asp.Net Mvc Internals &amp; ExtensibilityAsp.Net Mvc Internals &amp; Extensibility
Asp.Net Mvc Internals &amp; Extensibility
Eyal Vardi
 
Asp.net web api extensibility
Asp.net web api extensibilityAsp.net web api extensibility
Asp.net web api extensibility
Eyal Vardi
 
Models
ModelsModels
Models
Eyal Vardi
 
Web api routing
Web api routingWeb api routing
Web api routing
Eyal Vardi
 
Open Source, infrastructure as Code, Cloud Native Apps 2015
Open Source, infrastructure as Code, Cloud Native Apps 2015Open Source, infrastructure as Code, Cloud Native Apps 2015
Open Source, infrastructure as Code, Cloud Native Apps 2015
Jonas Rosland
 
Views
ViewsViews
Views
Eyal Vardi
 
Sst hackathon express
Sst hackathon expressSst hackathon express
Sst hackathon express
Aeshan Wijetunge
 
"Quantum" Performance Effects
"Quantum" Performance Effects"Quantum" Performance Effects
"Quantum" Performance Effects
Sergey Kuksenko
 
Mvc & java script
Mvc & java scriptMvc & java script
Mvc & java script
Eyal Vardi
 
MySQL Troubleshooting with the Performance Schema
MySQL Troubleshooting with the Performance SchemaMySQL Troubleshooting with the Performance Schema
MySQL Troubleshooting with the Performance Schema
Sveta Smirnova
 
Ektron CMS400 8.02
Ektron CMS400 8.02Ektron CMS400 8.02
Ektron CMS400 8.02
Alpesh Patel
 
Asp.net mvc filters
Asp.net mvc filtersAsp.net mvc filters
Asp.net mvc filtersEyal Vardi
 
Java ee7 1hour
Java ee7 1hourJava ee7 1hour
Java ee7 1hour
Frank Rodriguez
 
Moving applications to the cloud
Moving applications to the cloudMoving applications to the cloud
Moving applications to the cloud
Sergejus Barinovas
 
Oracle Application Framework Cases
Oracle Application Framework Cases Oracle Application Framework Cases
Oracle Application Framework Cases
Feras Ahmad
 
SignalR
SignalRSignalR
SignalR
Eyal Vardi
 
Shashi Raina [AWS] & Al Sargent [InfluxData] | Build Modern Monitoring with I...
Shashi Raina [AWS] & Al Sargent [InfluxData] | Build Modern Monitoring with I...Shashi Raina [AWS] & Al Sargent [InfluxData] | Build Modern Monitoring with I...
Shashi Raina [AWS] & Al Sargent [InfluxData] | Build Modern Monitoring with I...
InfluxData
 
clara-rules
clara-rulesclara-rules
clara-rules
Ikuru Kanuma
 
Creating and Using the Flux SQL Datasource | Katy Farmer | InfluxData
Creating and Using the Flux SQL Datasource | Katy Farmer | InfluxData Creating and Using the Flux SQL Datasource | Katy Farmer | InfluxData
Creating and Using the Flux SQL Datasource | Katy Farmer | InfluxData
InfluxData
 
Batch Applications for the Java Platform
Batch Applications for the Java PlatformBatch Applications for the Java Platform
Batch Applications for the Java Platform
Sivakumar Thyagarajan
 

Similar to Asp.net mvc internals & extensibility (20)

Asp.Net Mvc Internals &amp; Extensibility
Asp.Net Mvc Internals &amp; ExtensibilityAsp.Net Mvc Internals &amp; Extensibility
Asp.Net Mvc Internals &amp; Extensibility
 
Asp.net web api extensibility
Asp.net web api extensibilityAsp.net web api extensibility
Asp.net web api extensibility
 
Models
ModelsModels
Models
 
Web api routing
Web api routingWeb api routing
Web api routing
 
Open Source, infrastructure as Code, Cloud Native Apps 2015
Open Source, infrastructure as Code, Cloud Native Apps 2015Open Source, infrastructure as Code, Cloud Native Apps 2015
Open Source, infrastructure as Code, Cloud Native Apps 2015
 
Views
ViewsViews
Views
 
Sst hackathon express
Sst hackathon expressSst hackathon express
Sst hackathon express
 
"Quantum" Performance Effects
"Quantum" Performance Effects"Quantum" Performance Effects
"Quantum" Performance Effects
 
Mvc & java script
Mvc & java scriptMvc & java script
Mvc & java script
 
MySQL Troubleshooting with the Performance Schema
MySQL Troubleshooting with the Performance SchemaMySQL Troubleshooting with the Performance Schema
MySQL Troubleshooting with the Performance Schema
 
Ektron CMS400 8.02
Ektron CMS400 8.02Ektron CMS400 8.02
Ektron CMS400 8.02
 
Asp.net mvc filters
Asp.net mvc filtersAsp.net mvc filters
Asp.net mvc filters
 
Java ee7 1hour
Java ee7 1hourJava ee7 1hour
Java ee7 1hour
 
Moving applications to the cloud
Moving applications to the cloudMoving applications to the cloud
Moving applications to the cloud
 
Oracle Application Framework Cases
Oracle Application Framework Cases Oracle Application Framework Cases
Oracle Application Framework Cases
 
SignalR
SignalRSignalR
SignalR
 
Shashi Raina [AWS] & Al Sargent [InfluxData] | Build Modern Monitoring with I...
Shashi Raina [AWS] & Al Sargent [InfluxData] | Build Modern Monitoring with I...Shashi Raina [AWS] & Al Sargent [InfluxData] | Build Modern Monitoring with I...
Shashi Raina [AWS] & Al Sargent [InfluxData] | Build Modern Monitoring with I...
 
clara-rules
clara-rulesclara-rules
clara-rules
 
Creating and Using the Flux SQL Datasource | Katy Farmer | InfluxData
Creating and Using the Flux SQL Datasource | Katy Farmer | InfluxData Creating and Using the Flux SQL Datasource | Katy Farmer | InfluxData
Creating and Using the Flux SQL Datasource | Katy Farmer | InfluxData
 
Batch Applications for the Java Platform
Batch Applications for the Java PlatformBatch Applications for the Java Platform
Batch Applications for the Java Platform
 

More from Eyal Vardi

Why magic
Why magicWhy magic
Why magic
Eyal Vardi
 
Smart Contract
Smart ContractSmart Contract
Smart Contract
Eyal Vardi
 
Rachel's grandmother's recipes
Rachel's grandmother's recipesRachel's grandmother's recipes
Rachel's grandmother's recipes
Eyal Vardi
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2Performance Optimization In Angular 2
Performance Optimization In Angular 2
Eyal 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 NgModule
Eyal 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.x
Eyal Vardi
 
Angular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time CompilationAngular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time Compilation
Eyal Vardi
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And Navigation
Eyal Vardi
 
Angular 2 Architecture
Angular 2 ArchitectureAngular 2 Architecture
Angular 2 Architecture
Eyal 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.x
Eyal Vardi
 
Angular 2.0 Views
Angular 2.0 ViewsAngular 2.0 Views
Angular 2.0 Views
Eyal 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.0
Eyal Vardi
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0Template syntax in Angular 2.0
Template syntax in Angular 2.0
Eyal Vardi
 
Http Communication in Angular 2.0
Http Communication in Angular 2.0Http Communication in Angular 2.0
Http Communication in Angular 2.0
Eyal Vardi
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injectionAngular 2.0 Dependency injection
Angular 2.0 Dependency injection
Eyal Vardi
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and Navigation
Eyal Vardi
 
Async & Parallel in JavaScript
Async & Parallel in JavaScriptAsync & Parallel in JavaScript
Async & Parallel in JavaScript
Eyal Vardi
 
Angular 2.0 Pipes
Angular 2.0 PipesAngular 2.0 Pipes
Angular 2.0 Pipes
Eyal Vardi
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
Eyal Vardi
 

More from 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
 

Recently uploaded

GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 

Recently uploaded (20)

GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 

Asp.net mvc internals & 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. ASP.NET vs. ASP.NET MVC © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. URL Routing Pipeline © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. Route lifecycle © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 14. Routing Process Route MvcRouteHandler MvcHandler Controller IDependencyResolver © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 15. Custom Route Constraint © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 16. Routing Summary  URL Templates  Default values  Namespaces  Constraint  IRouteHandler  RouteBase © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 17. Dependency Resolver  Service locator for the framework. protected void Application_Start() { ... DependencyResolver.SetResolver(new TraceDependencyResolver() ); } © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 18. Trace Dependency Resolver © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 19. Dependency Resolver © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 20. Controller Extensibility © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 21. Understanding Controllers  MVC controllers are responsible for responding to requests made against an ASP.NET MVC website. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 22. Controller Class © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 23. Creation of the Controller  IControllerFactory Responsible for determining the controller to service the request.  IControllerActivator Responsible for creating the controller  DefaultControllerFactory has internal class called DefaultControllerActivator. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 24. Creation of the Controller Request Routing MvcRouteHandler MvcHandler Dependency Controller Dependency Controller Resolver Resolver Controller Factory Activator © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 25. How Controller Created? © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 26. Controller Summary  MvcHandler  IControllerFactory  IControllerActivator © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 27. The Action Execution Request Routing MvcRouteHandler MvcHandler Dependency Controller Dependency Controller Resolver Resolver Controller Factory Activator Action Action Invoker Method © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 28. The Action Execution  Once the controller has been instantiated, the specified action has to be executed, and this is handled by the IActionInvoker.  If you derived your controller from the Controller class, then this is the responsibility of an action invoker. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 29. The Action Execution © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 30. Built-In Action Invoker  Finds a method that has the same name as the requested action. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 31. Custom Action Name  You can then accept an action name that wouldn’t be legal as a C# method name.  [ActionName("User-Registration")]  [ActionNameSelector] © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 32. Action Method Selection  The action invoker uses an action method selector to remove ambiguity when selecting an action.  The invoker gives preference to the actions that have selectors. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 33. Custom Action Method Selector  You return true from IsValidForRequest if the method is able to process a request, and false otherwise. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 34. Custom Method Selector © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 35. Handling Unknown Actions © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 36. Action Summary  IActionInvoker  Action Name Selector  Action Method Selector  Handling Unknown Actions © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 37. Model Extensibility © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 38. What Is a Model? Model Binding Model Deserialization HTML View Controller HTTP Template © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 39. HTTP to .NET Classes public ActionResult Index(Reservation reservation) { // TODO BL. return View( reservation ); } Deserialization © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 40. HTTP to .NET Args © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 41. 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
  • 42. DefaultModelBinder Class  Translate HTTP request into complex models, including nested types and arrays.  URL  Form data, Culture - Sensitive 1. Request.Form 2. RoutedData.Values (Key,Value) 3. Request.QueryString 4. Request.Files © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 43. Model Binder Types  ByteArrayModelBinder  LinqBinaryModelBinder  FormCollectionModelBinder  HttpPostedFileBaseModelBinder  DefaultModelBinder © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 44. Binding to Complex Types [DisplayName("Book")] Model public class E4DBook { [Display(Name = "Title")] public string E4DTitle { get; set; } public Reservation MyReservation { get; set; } } @Html.LabelFor(model => model.MyReservation.FlightNum) View @Html.EditorFor(model => model.MyReservation.FlightNum) Html <label for="MyReservation_FlightNum">FlightNum</label> <input type="text" value="" id = "MyReservation_FlightNum" name = "MyReservation.FlightNum" /> © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 45. Specifying Custom Prefixes Controller public ActionResult Index( Reservation res1 , [Bind(Prefix="myRes")] Reservation res2 ) { ... } <!– First Arg --> Html <label for="FlightNum">FlightNum</label> <input type="text" id="FlightNum" name="FlightNum" /> ... <!– Second Arg --> <label for="MyRes_FlightNum">FlightNum</label> <input type="text" id="MyRes_FlightNum" name="MyRes.FlightNum" /> © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 46. Bind Attribute (“View”)  Represents an attribute that is used to provide details about how model binding to a parameter should occur. [Bind(Include = "FlightNum, TravelDate)] public class Reservation { public int FlightNum { get; set; } public DateTime TravelDate { get; set; } public int TicketCount { get; set; } public int DiscountPercent { get; set; } } © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 47. Bind Attribute © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 48. Binding to a String Collections Controller public ActionResult Index( List< string > reservation ) { ... } Html <input type="text" name="reservation" /> <input type="text" name="reservation" /> <input type="text" name="reservation" /> <input type="text" name="reservation" /> © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 49. Binding to a Collections Controller public ActionResult Index( List<Reservation> reservation ) { ... } <!– Option I--> Html <input type="text" name="[0].Name" /> <input type="text" name="[1].Name" /> <!– Option II --> <input type="hidden" name="Index" value="f1" /> <input type="text" name="[f1].Name" /> <input type="hidden" name="Index" value="f2" /> <input type="text" name="[f2].Name" /> © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 50. Binding to a Dictionary Controller public ActionResult Index( Dictionary<Reservation> reservation ) { ... } Html <input type="hidden" name="[0].key" value="f1" /> <input type="text" name="[0].value.Name" /> <input type="hidden" name="[1].key" value="f2" /> <input type="text" name="[1].value.Name" /> © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 51. 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
  • 52. 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
  • 53. Custom Binder © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 54. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 55. Model Summary  Http to .NET Classes  IModelBinder  IValueProvider  ModelMetadataProvider  BindAttribute  Validation  Data Annotations  IValidateObject © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 56. Action Results  A controller action returns something called an action result.  Action results types:  ViewResult  FileContentResult  EmptyResult  FilePathResult  RedirectResult  FileStreamResult  JsonResult  HttpNotFoundResult  JavaScriptResult  HttpRedirectResult  ContentResult  HttpStatusCodeResult © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 57. Custom Action Result © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 58. 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 { [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
  • 59. Filter Interfaces Action Method Action Result © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 60. 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
  • 61. 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
  • 62. 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
  • 63. 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
  • 64. 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
  • 65. 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
  • 66. Handle Error Attribute  You can specify an exception and the names of a view and layout.  Works only when custom errors are enabled in the Web.config file  <customErrors mode="On" /> inside the <system.web>  The view get HandleErrorInfo © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 67. First Global Controller Action Last Filter I Filter I Filter I Filter I Filter I Filter II Filter II Filter II Filter II Filter II Action Method Action Result Authorization Action Result Exception Filter I Filter I Filter I Filter I Filter I Filter II Filter I Filter II Filter I Filter II Filter I Filter I Filter I Filter I Filter I Filter II Filter I Filter II Filter I Filter I Filter I Filter I Filter II Filter II Filter I Filter I Filter I Filter I Filter II Filter I Filter I Filter II Filter I Filter II Filter I Filter I Filter I Filter I Filter I Filter I © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 68. Action Before After Filter I Filter II Filter I Filter II Filter I Filter II Filter I Filter II Filter I Filter II Filter II Filter I Filter II Filter I Filter II Filter I Filter II Filter I Filter II Filter I © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 69. Controller Context 1 6 2 3 4 5 © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 70. Filters © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 71. 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
  • 72. Custom Filter © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 73. The Filter Provider Interface © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 74. 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
  • 75. Filter Summary  Custom Filters  IAuthorizationFilter  IActionFilter  IResultFilter  IExceptionFilter  IFilterProvider © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 76. View Extensibility © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 77. Creating a Custom View Engine © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 78. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 79. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 80. View Summary  IViewEngine  How to find the view?  IView  Render © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 81. MVC Internals & Extensibility Summary Mvc Routing RouteHandler MvcHandler Dependency Controller Dependency Controller Resolver Resolver Controller Factory Activator Action Name Method Authorization Model Invoker Selector Selectror Filter Bind Action Action Action Result View Filer Method Filer Filter Engine Result Exception View Filter Filter © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il