Sunteți pe pagina 1din 75

Extending ASP.

NET MVC
Part 1

Simone Chiaretta
Architect, Council of the EU
http://codeclimber.net.nz
Twitter: @simonech
June 23rd, 2010
Who the hell am I?
► Simone Chiaretta
► Microsoft MVP ASP.NET
► ASP Insider
► Blogger – http://codeclimber.net.nz
► Italian ALT.NET UG Founder
► OpenSource developer
► Climber
► All Around Nice Guy

Disclaimer:"The views expressed are purely those of the speaker and may not in any circumstances be regarded as stating an official position of the Council"
Agenda
► The default ASP.NET MVC Pipeline
► Extensibility points’ list
► The Most important extensibility points
The Default ASP.NET MVC Pipeline
The default ASP.NET MVC Pipeline

Request Controller
Controller
Factory

URL Http Response


Routing Action
Handler

Route View
Route View
Handler Factory

5
The default components
► URL Routing:
– Parses the URL, and instantiate the MvcHandler
► Controller Factory
– Takes URL parameters, create controller via
reflection based on Controller name
► Action Invoker
– Invokes the action based on the name, with the
filters before and after
► View Engine
– WebForms view engine
► Template
– Renders a TextBox almost for everything
► HtmlHelper
– Has a bunch of standard methods
ASP.NET MVC IS extensible
► If you don’t like them
► If you need something else
► Change them
ASP.NET MVC IS extensible
► Almost every aspect of the framework can be
extented/replaced
Extensibility points’ list
Routing extensibility
► RouteConstraint
– Validates route parameters with code
► RouteHandler
– Defines how the request must be handled
Controller Extensibility
► ControllerFactory
– Responsible for creating Controllers
► ActionInvoker
– Invokes an action, based only on its name
► ActionMethodSelector Attribute
– Helps the action invoker decide which action to
invoke when the name is not unique
► Controller
– The base class for every controller
► ActionResult
– Decides how to send the output to the user
Action Filter Extensibility
► AuthorizationFilter
– Makes sure the current request is allowed
► Action Filters
– Executed before or after the action executes
► Result Filters
– Executed before or after an action result executes
Binding/Validation Extensibility
► ModelBinder
– Populates the action method parameters from the
request
► ModelValidator Provider
– Retrieves the validation rules
► Server-side Validation Rules
– The actual server-side validation rule
View Extensibility
► ViewEngine
– The service that transforms in HTML the data for the
user
► HtmlHelpers
– Utility functions that hide away the generation of
some HTML markup or JavaScript code
► Client-side Validation Rules
– Client-side validation rules
► ModelMetadata Provider
– Retrieves the metadata needed for the templated
helpers
► Custom Templates
– Renders the html to edit/display specific types
The Most important extensibility points
The Most important Extensibilit points
► The ones you will definitely use
► The ones you have to start using
► The ones you might or might not need to use
► The ones you most likely will not write
yourself
– but probably use written by others
Extensibility points you will use
Custom Templates
Custom Templates
► WHAT: Renders the html to edit/display
specific types
► DEFAULT: Everything is a label or a textbox
► WHY: Add your own to customize specific data-
types
Custom Templates
► Add PartialViews in:
– /Views/Shared/DisplayTemplates
– /Views/Shared/EditorTemplates

Demo
Server-side Validation Rules
Server-side Validation Rules
► WHAT: Define how a property is validated in
the server side validation
► DEFAULT: Required, Length, wrong type
► WHY: Add your own rules
Server-side Validation Rules
► Write a new ValidationAttribute
► Implement the IsValid method
► Apply attribute to your model

Demo
Client-side Validation Rules
Client-side Validation Rules
► WHAT: Define how a property is validated in
the client-side validation
► DEFAULT: Client-side counterparts of the
default server-side validators
► WHY: Add your own validators
Client-side Validation Rules
► First make a server-side validator
► Make validation logic in JavaScript
► Write adapter to push validation rules to
client-side
► Register validation function via JS
► Register adapter in Global.asax

Demo
Extensibility points you have to start
using
Base Controller
Base Controller
► WHAT: The base class for every Controller
► DEFAULT: Default implementation of helper
methods
► WHY: Extend if you want to enforce you own
conventions
Base Controller
► Override Controller
► Your controllers override from BaseController
instead of Controller

Demo
HtmlHelpers
HtmlHelpers
► WHAT: Utility functions that hide away the
generation of HTML markup or JavaScript code
► DEFAULT: Html.TextBox, Html.Encode,
Html.Partial, …
► WHY: “If there is an if, write an Helper”
HtmlHelpers
► Add new methods as extension methods

Demo
Rating
If you liked this talk, please consider rating
it:
http://speakerrate.com/talks/3669-asp-net-mvc-extensibility

34 Disclaimer:"The views expressed are purely those of the speaker and may not in any circumstances be regarded as stating an
official position of the Council"
Extending ASP.NET MVC
Part 2

Simone Chiaretta
Architect, Council of the EU
http://codeclimber.net.nz
Twitter: @simonech
June 23rd, 2010
Filters
Authorization Filter
► WHAT: Makes sure the current request is
allowed
► DEFAULT: It is based on the ASP.NET
Membership provider
► WHY: Change if you want not to use ASP.NET
MVC or if you want to enhance the route
dictionary
Action Filters
► WHAT: Executed before or after the action
executes
► DEFAULT: Output cache
► WHY: Add your own based on your needs
Result Filters
► WHAT: Executed before or after an action
result executes
► DEFAULT: No default result filters
► WHY: Add your own based on your needs
Authorization Filter
► Implement IAuthorizationFilter
– OnAuthorization
Filters
► Implement IActionFilter + IResultFilter
► Make new ActionFilterAttribute
– OnActionExecuting
– OnActionExecuted
► Implement IResultFilter
– OnResultExecuting
– OnResultExecuted
Extensibility points you might want to
use
RouteConstraint
RouteConstraint
► WHAT: Validates route parameters
► DEFAULT: No default implementation
► WHY: Add your own when needed
RouteConstraint
► Implement
– IRouteConstraint.Match
► Inspect request and return true or false

Demo
RouteHandler
RouteHandler
► WHAT: Defines how the request must be handled
► DEFAULT: Routes the request to MvcHandler
► WHY: Change if you want not to use ASP.NET
MVC
RouteHandler
► Implement
– IRouteHandler.GetHttpHandler

Demo
ActionMethodSelector Attribute
ActionMethodSelector Attribute
► WHAT: Helps the action invoker decide which
action to invoke when the name is not unique
► DEFAULT: HttpMethod attributes: decide based
on the HttpMethod
► WHY: Add your own to support different
scenarios
ActionMethodSelector Attribute
► Create new Attibute inheriting from
– ActionMethodSelectorAttribute
► Implement:
– IsValidForRequest

Demo
ActionResult
ActionResult
► WHAT: Sends the output to the user
► DEFAULT: ViewResult, RedirectResult,
FileResult, JsonResult, etc…
► WHY: Add your own if you need an output not
available in the default results
ActionResult
► Create a new result inheriting from:
– ActionResult
► Implement:
– ExecuteResult
► Optionally create an helper method in you
“base controller”

Demo
ModelBinder
ModelBinder
► WHAT: Populates the action method parameters
from the request
► DEFAULT: Binds request’s values based on
names
► WHY: Extend if you need to add other way of
binding
ModelBinder
► Implement IModelBinder
– BindModel
► Inherit from DefaultModelBinder
– BindProperty
– OnModelUpdated
– OnModelUpdating
► Register the ModelBinder

Demo
Very unlikely to write your own
ControllerFactory
Controller Factory
► WHAT: Responsible for creating Controllers
► DEFAULT: Create an instance of the Controller
via Reflection based on its name
► WHY: Change if you want to create the
controller in other ways or if you want to
add some funtionalities
► Most of the main IoC container have a custom
ControllerFactory
Controller Factory
► Implement IControllerFactory
– CreateController
– ReleaseController
► Override DefaultControllerFactory
– GetControllerInstance(Type controllerType)

Demo
ActionInvoker
ActionInvoker
► WHAT: Invokes an action, based only on its
name
► DEFAULT: Call the action method, via
reflection, based on its name, gathering
filters via attributes and calling them
before and after
► WHY: Change if you want to change the way
methods are called, or want different way to
configure Filters
ActionInvoker
► Implement IActionInvoker
– InvokeAction
► Override ControllerActionInvoker
– InvokeActionMethodWithFilters
– InvokeActionResultWithFilters
– GetFilters
– …

Demo
ViewEngine
View Engine
► WHAT: The service that transforms in HTML the
data for the user
► DEFAULT: WebForm view engine
► WHY: Change if you don’t like the WebForm
approach
View Engine
► IViewEngine:
– Usually override VirtualPathProviderViewEngine
– CreateView
► IView:
– Render

Demo
ModelValidator Provider
ModelValidator Provider
► WHAT: Retrieves the validation rules
► DEFAULT: Validation rules are defined with
DataAnnotation attributes
► WHY: Change if you want to specify validation
rules in other ways (XML, Database, etc…)
ModelValidator Provider
► Override from ModelValidatorProvider
► Implement GetValidators
ModelMetadata Provider
ModelMetadata Provider
► WHAT: Retrieves the metadata needed for the
templated helpers
► DEFAULT: Metadata are defined with
DataAnnotation attributes
► WHY: Change if you want to specify metadata
in other ways (XML, Database, etc…)
ModelMetadata Provider
► Inherit from ModelMetadataProvider
► Implement
– GetMetadataForType
– GetMetadataForProperty
– GetMetadataForProperties
Contacts – Simone Chiaretta
► MSN: simone_ch@hotmail.com
► Blog:
– English: http://codeclimber.net.nz/
– Italian: http://blogs.ugidotnet.org/piyo/
► Twitter: @simonech

74
Rating
If you liked this talk, please consider rating
it:
http://speakerrate.com/talks/3669-asp-net-mvc-extensibility

75 Disclaimer:"The views expressed are purely those of the speaker and may not in any circumstances be regarded as stating an
official position of the Council"

S-ar putea să vă placă și