[-] Controller contains application logic. Servers as a bridge between the Web and your domain model.
[-] All controllers inherited from System.Web.Mvc.Controller, which implemet IController interface.
[-] Public method is called action method, which is mapped and can be invoked by a public URL.
[-] Action method is invoked with parameters extracted from the incoming request, including HTTP Get/Post data, route data…
[-] Action method returns Action results, which are objects describing the intended result of an action (e.g., rendering a view, or redirecting to a different URL oraction method).
[-] Get user input
- Directly from context Objects: Request.QueryString["paraName"] for HTTP Get data; Request.Form["paraName"] for HTTP Post Data
- Using Action Method Parameters: framework will use the parameter name (case sensitive) as key to search Request.QueryString, Request.Form, and RouteData.Values key-value dictionary and populate the value.
- Model Binding: the action parameter is an object. MVC will populate the object property using the context data with the same name.
[-] If the framework can’t find any match for a particular parameter, it will try to supply null for that parameter, which will cause exception for value type like int. Value-type parameters are inherently compulsory. Reference-type parameters are inherently optional.
[-] Pass data to view
- ViewData, key-value(any object) pair dictionary.
- ViewData.Model object
[-] Performing Redirections using RedirectToAction, Redirect
[-] Using TempData to Preserve Data Across a Redirection. TempData automatically removes themselves from the collection after you read them back.
[-] Returning Textual Data: XML, JSON, CSV, Plain text, JavaScript Commands using ContentResult type.
[-] Return Files and Binary Data
[-] using Filters(.net attributes) to Attach Reusable Behaviors: Authorization filter, Action filter, Result filter, Exception filter
[-] use Asynchronous Controllers to boost Server Capacity
ASP.Net MVC Controller Summary
February 6, 2011 by zhenglinet
Advertisement