Nov 10, 2009

Extension point in asp.net mvc

  1. Extending route.

    Route matching is first step mvc framework, we can customize how route is matched by adding constraint, and default, and dataToken. We can implement IRouteConstraint. Here is ASP.NET MVC Tip #30 – Create Custom Route Constraints

  2. Extending IRouteHandler

    When we register route, by default the route handler is MvcRouteHandler. We can create a class implmenting IRouteHandler, of we can sub class MvcRouteHandler

  3. Exending MvcHandler

    MvcHandler is IHttpHandler, we can substitute with a MvcHandler sub class, or a complete new IHttpHandler , or override MvcHandler's ProcessRequest method. We can override ProcessRequest(HttpContextBase httpContext) method.

  4. Extending ControllerFactory and Controller. We can decide how a controller is created. A dummy example is as follow. But you can create a controller by using IoC container.

    ControllerBuilder.Current.SetControllerFactory(new ObjectControllerFactory());
    
    public class ObjectControllerFactory:DefaultControllerFactory
    {
    
        public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            if (controllerName.ToLower() == "object")
            {
                return new ObjectController(new object());
            }
            else
            {
                return base.CreateController(requestContext, controllerName);
            }
        }
    
    }
    
  5. Extending the ActionResult.

  6. Extending the Controller.

  7. Extending ModelBinder

  8. Extending ViewResult

  9. Extending ViewEngine, and IView

    We can subclass the default WebFormViewEngine, or implement and IViewEngine. The interesting method is FindView and FindPartialView.

  10. Extending ViewPage or ViewUserControl

  11. Extending HtmlHelper by adding HtmlHelper extension method.