Adding Actions Model Binding Filters Vanity URLs Controller Best Practices Taking Control of Controllers Adding Actions Model Binding Filters Vanity URLs Controller Best Practices Adding Actions ID: 731251
Download Presentation The PPT/PDF document "Controllers Taking Control of Controller..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
ControllersSlide2
Taking Control of Controllers
Adding Actions
Model Binding
Filters
Vanity URLs
Controller Best PracticesSlide3
Taking Control of Controllers
Adding Actions
Model Binding
Filters
Vanity URLs
Controller Best PracticesSlide4
Adding Actions
Controllers are classes
Actions are methods
Creating an action involves adding a method to a classSlide5
Action Signature
Return Types
ActionResult
FileResult
JsonResult
ViewResult
Parameters
Normal parameters
MCV model bindingSlide6
Controller Method Attributes
HttpGet
&
HttpPost
Known as methods or verbs
Send a signal from the client to the server
Header information that lets
user send
large amounts of dataSlide7
Get and Post
Create/Update/Delete are typically two step operations
Present the form
Accept the input
Create two actions
Form presentation via
HttpGet
(default)
Accept data via
HttpPostSlide8
Controllers
All the controller classes inherit from the base class Controller
Has built in helper methods such as View()
Parameter to the View method is the data that the view needs
Controller method gets the model and combines the model with the view to return
public
ActionResult
Index()
{
return View(
db.Movies.ToList
());
}Slide9
Default Model Binder
Uses the name attribute of input elements
Automatically matches parameter names for simple data types
Complex objects are mapped by property name
Complex properties use dot
notation
@
Html.EditorFor
(model => model.Lyrics
, …Slide10
Controlling Model Binding
Imagine the following model
Need
Create a form to edit everything but the lyrics
Challenge
Default model binder automatically binds all inbound propertiesSlide11
Solutions
Simplest
Use the bind attribute to indicate which properties to bind
Other solutions
Create a view model
Create a custom model binderSlide12
Filters
Filters are attributes
Decorate controllers and actions
Goal is to alter execution
MVC contains several built-in filters
If
you want the filter to be global, put it in the
FilterConfig.cs
fileSlide13
Security Filters
Authorize
Control who can access a controller/action
Properties
Users
Roles
ValidateAntiForgeryToken
Defends against cross-site request forgery
Requires anti-forgery token to be added to view
RequireHttps
Uses SSLSlide14
SSL
Encrypts traffic and prevents tampering
Authenticates server
When to use SSL
Asking for sensitive information
After authentication
When in doubt enable SSL
http://blog.codinghorror.com/should-all-web-traffic-be-encrypted/Slide15
Vanity URL
Standard URL
Users have no idea what that URL refers to
Search engines have no idea what that URL refers to
Vanity URL
User knows information provided by the page
Search engines know information provided by pageSlide16
MVC Routing
Vanity URLs are handled by routing
Routing in MVC controls what controller/action is called based on the URL provided
Methods for updating routing
RouteConfig.cs
AttributeRoutingSlide17
Attribute Routing
Attributes control routing/URL
RouteAttribute
www.mymusicstore.com/Album/Edit/42
Calls the Edit action
Passes in the ID parameter
ID must be an integerSlide18
RoutePrefix
Added to controller
Adds prefix to all routesSlide19
Controller Design Guidelines
High Cohesion
Make sure all actions are closely related
Low Coupling
Controllers should know as little about the rest of the system as possible
Simplifies testing and changes
Repository pattern
Wrap data context calls into another object