This document summarizes key aspects of ASP.NET MVC including controllers, actions, returning different result types from actions, passing data to views, using filters, and more. Controllers are responsible for controlling application flow and exposing public methods as actions. Actions can return various result types including ViewResult, RedirectResult, ContentResult, and JsonResult. Data can be passed to views from controllers using ViewData, ViewBag, TempData, and strongly typed models. Filters provide a way to inject extra behaviors into controllers and actions, and common filter types include AuthorizeFilter, HandleErrorFilter, and OutputCacheFilter.
3. Controllers Responsible for controlling the flow of application Exposes public methods as actions Each action returns different results types Inherits from System.Web.Mvc.Controller
11. Action Method Parameters – 1 public ActionResult ShowInfo(string city) { // Equivalent to var tempCity = Request.Form[‘’city’’]; } Optional Parameters Nullable types DefaultValueAttribute Optional Parameters with C# 4.0 Complex Parameters public ActionResult Update(Product p) { .... }
12. Action Method Parameters – 2 Invoking model binding manually public ActionResult Update(int productID) { var product = repository.Get(productID); UpdateModel(product); repository.Update(product); return View(product); }
13. Passing Data to Views – 1 Controllers and views are totally independent Controllers suply data to views No way to access controllers from views Supplying data from a controller ViewData[‘’product’’] = productObject; Accessing data from the view <%: ((Product)ViewData[‘’product’’]).Name %>
14. Passing Data to Views – 2 Sending strongly typed objects to views public ActionResult ProductInfo(int id) { var product = repository.Get(id); return View(product); } // View.aspx Product Name: <%: Model.Name %>
15. Passing Data to Views – 3 Passing dynamic objects to views public ActionResult ProductDetails(int id) { dynamic model = new ExpandoObject(); model.Product = repository.Get(id); model.Message = ‘’Out of Stock’; return View(model); } // View.aspx <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> Product: <%: Model.Product.Name %> Message: <%: Model.Message %>
16. Using TempData Similar usage with ViewData Preserves data across redirections public ActionResult Update(Product product) { repository.Update(product); TempData[‘’message’’] = ‘’Product ‘’ + product.Name + ‘’ updated.’’; return RedirectToAction(‘’Success’’); } // Success action view.aspx <% if (TempData[‘’message’’] != null) %> <p><%: TempData[‘’message’’] %></p> <% } %>
17. Using Filters Injects extra behaviors to controller and actions Derive from FilterAttribute class Basic types of filters
19. How Filters are Executed try { Run each IAuthorizationFilter'sOnAuthorization() method if(none of the IAuthorizationFilters cancelled execution) { Run each IActionFilter'sOnActionExecuting() method Run the action method Run each IActionFilter'sOnActionExecuted() method (in reverse order) Run each IResultFilter'sOnResultExecuting() method Run the action result Run each IResultFilter'sOnResultExecuted() method (in reverse order) } else { Run any action result set by the authorization filters } } catch(exception not handled by any action or result filter) { Run each IExceptionFilter'sOnException() method Run any action result set by the exception filters }
21. Authorize Filter Run early in the request Users property Roles property Order property [Authorize(Roles=‘’SalesRep’’, Users=‘’onsela’’)] public ActionResult ProductList() { return View(); }
22. HandleError Filter Detects exceptions Renders a specific view Returns HTTP status code 500 to clients [HandleError(View=‘’ErrorPage’’, ExceptionType=typeof(SqlException)] public ActionResult ProductList() { return View(); }