ASP.NET MVC<br />ÖNSEL AKIN<br />
Controllers & Actions<br />
Controllers<br />Responsible for controlling the flow of application<br />Exposes public methods as actions<br />Each acti...
Returning Action Results<br />ViewResult			View()<br />PartialViewResult		PartialView()<br />RedirectResult			Redirect()<b...
Returning View Results<br />Returns HTML to the browser<br />Implicit vs Explicit view naming<br />return View();<br />ret...
Returning Redirect Results<br />Same controller<br />return RedirectToAction(‘’Index’’);<br />Different controller<br />re...
Returning Content Results<br />return Content(‘’Hello’’);<br />Returning .Net types<br />public string HelloAction() {<br ...
Returning Json Results<br />Returns result in JavaScript Object Notation (JSON) format<br />Uses JavaScriptSerializer<br /...
Returning JavaScript<br />public ActionResult ShowMessage() {<br />return JavaScript(‘’alert(‘Message!’);’’);<br />}<br />...
Accessing Request Information<br />
Action Method Parameters – 1<br />public ActionResult ShowInfo(string city) {<br />	// Equivalent to<br />var tempCity = R...
Action Method Parameters – 2<br />Invoking model binding manually<br />public ActionResult Update(int productID) {<br />va...
Passing Data to Views – 1<br />Controllers and views are totally independent<br />Controllers suply data to views<br />No ...
Passing Data to Views – 2<br />Sending strongly typed objects to views<br />public ActionResult ProductInfo(int id) {<br /...
Passing Data to Views – 3<br />Passing dynamic objects to views<br />public ActionResult ProductDetails(int id) {<br />dyn...
Using TempData<br />Similar usage with ViewData<br />Preserves data across redirections<br />public ActionResult Update(Pr...
Using Filters<br />Injects extra behaviors to controller and actions<br />Derive from FilterAttribute class<br />Basic typ...
Applying Filters<br />[Authorize(Roles=‘’Administrator’’)]<br />public class ProductController : Controller {<br />	[Outpu...
How Filters are Executed<br />try<br />{<br />Run each IAuthorizationFilter'sOnAuthorization() method<br />if(none of the ...
IActionFilter, IResultFilter Methods<br />
Authorize Filter<br />Run early in the request<br />Users property<br />Roles property<br />Order property<br />[Authorize...
HandleError Filter<br />Detects exceptions<br />Renders a specific view<br />Returns HTTP status code 500 to clients<br />...
OutputCache Filter<br />
Handling Unknown Actions<br />public class HomeController : Controller<br />{<br />	protected override void HandleUnknownA...
Upcoming SlideShare
Loading in …5
×

ASP.NET MVC Controllers & Actions

6,650 views

Published on

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,650
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
158
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

ASP.NET MVC Controllers & Actions

  1. 1. ASP.NET MVC<br />ÖNSEL AKIN<br />
  2. 2. Controllers & Actions<br />
  3. 3. Controllers<br />Responsible for controlling the flow of application<br />Exposes public methods as actions<br />Each action returns different results types<br />Inherits from System.Web.Mvc.Controller<br />
  4. 4. Returning Action Results<br />ViewResult View()<br />PartialViewResult PartialView()<br />RedirectResult Redirect()<br />ContentResult Content()<br />JsonResult Json()<br />FileResult File()<br />EmptyResult <br />HttpUnauthorizedResult<br />JavaScriptResult JavaScript()<br />RedirectToRouteResult RedirectToRoute()<br />
  5. 5. Returning View Results<br />Returns HTML to the browser<br />Implicit vs Explicit view naming<br />return View();<br />return View(‘’ViewName’’)<br />Specifying paths<br />return View(‘’SubFolder/ViewName’’)<br />return View(‘’~/View.aspx’’)<br />
  6. 6. Returning Redirect Results<br />Same controller<br />return RedirectToAction(‘’Index’’);<br />Different controller<br />return RedirectToAction(‘’Product’’, ‘’List’’);<br />Providing route values<br />return RedirectToAction(‘’Product’’, ‘’Details’, new { id = 20 });<br />
  7. 7. Returning Content Results<br />return Content(‘’Hello’’);<br />Returning .Net types<br />public string HelloAction() {<br />return ‘’Hello’’;<br />}<br />ToString() and wrapping with ContentResult<br />
  8. 8. Returning Json Results<br />Returns result in JavaScript Object Notation (JSON) format<br />Uses JavaScriptSerializer<br />{ <br />id: 10, name: ‘SharePoint 2010’, <br />authors: [{ ‘onsela’, ‘mehmeta’ }]<br />}<br />var books = new List<Book>();<br />return Json(books);<br />
  9. 9. Returning JavaScript<br />public ActionResult ShowMessage() {<br />return JavaScript(‘’alert(‘Message!’);’’);<br />}<br />// View.aspx<br /><%: Ajax.ActionLink(‘’Show message’’, ‘’ShowMessage’’, null) %><br />
  10. 10. Accessing Request Information<br />
  11. 11. Action Method Parameters – 1<br />public ActionResult ShowInfo(string city) {<br /> // Equivalent to<br />var tempCity = Request.Form[‘’city’’];<br />}<br />Optional Parameters<br />Nullable types<br />DefaultValueAttribute<br />Optional Parameters with C# 4.0<br />Complex Parameters<br />public ActionResult Update(Product p) { .... }<br />
  12. 12. Action Method Parameters – 2<br />Invoking model binding manually<br />public ActionResult Update(int productID) {<br />var product = repository.Get(productID);<br />UpdateModel(product);<br />repository.Update(product);<br />return View(product);<br />}<br />
  13. 13. Passing Data to Views – 1<br />Controllers and views are totally independent<br />Controllers suply data to views<br />No way to access controllers from views<br />Supplying data from a controller<br />ViewData[‘’product’’] = productObject;<br />Accessing data from the view<br /><%: ((Product)ViewData[‘’product’’]).Name %><br />
  14. 14. Passing Data to Views – 2<br />Sending strongly typed objects to views<br />public ActionResult ProductInfo(int id) {<br />var product = repository.Get(id);<br />return View(product);<br />}<br />// View.aspx<br />Product Name: <%: Model.Name %><br />
  15. 15. Passing Data to Views – 3<br />Passing dynamic objects to views<br />public ActionResult ProductDetails(int id) {<br />dynamic model = new ExpandoObject();<br />model.Product = repository.Get(id);<br />model.Message = ‘’Out of Stock’;<br />return View(model);<br />}<br />// View.aspx<br /><%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %><br />Product: <%: Model.Product.Name %><br />Message: <%: Model.Message %><br />
  16. 16. Using TempData<br />Similar usage with ViewData<br />Preserves data across redirections<br />public ActionResult Update(Product product) {<br />repository.Update(product);<br />TempData[‘’message’’] = ‘’Product ‘’ + product.Name + ‘’ updated.’’;<br />return RedirectToAction(‘’Success’’);<br />}<br />// Success action view.aspx<br /><% if (TempData[‘’message’’] != null) %><br /><p><%: TempData[‘’message’’] %></p><br /><% } %><br />
  17. 17. Using Filters<br />Injects extra behaviors to controller and actions<br />Derive from FilterAttribute class<br />Basic types of filters<br />
  18. 18. Applying Filters<br />[Authorize(Roles=‘’Administrator’’)]<br />public class ProductController : Controller {<br /> [OutputCache(Duration=30)]<br />public ActionResult Save(Product p) {<br />}<br />}<br />
  19. 19. How Filters are Executed<br />try<br />{<br />Run each IAuthorizationFilter'sOnAuthorization() method<br />if(none of the IAuthorizationFilters cancelled execution)<br /> {<br />Run each IActionFilter'sOnActionExecuting() method<br /> Run the action method<br />Run each IActionFilter'sOnActionExecuted() method (in reverse order)<br />Run each IResultFilter'sOnResultExecuting() method<br /> Run the action result<br />Run each IResultFilter'sOnResultExecuted() method (in reverse order)<br /> }<br /> else<br /> {<br />Run any action result set by the authorization filters<br /> }<br />}<br />catch(exception not handled by any action or result filter)<br />{<br />Run each IExceptionFilter'sOnException() method<br />Run any action result set by the exception filters<br />}<br />
  20. 20. IActionFilter, IResultFilter Methods<br />
  21. 21. Authorize Filter<br />Run early in the request<br />Users property<br />Roles property<br />Order property<br />[Authorize(Roles=‘’SalesRep’’, Users=‘’onsela’’)]<br />public ActionResult ProductList() {<br /> return View();<br />}<br />
  22. 22. HandleError Filter<br />Detects exceptions<br />Renders a specific view<br />Returns HTTP status code 500 to clients<br />[HandleError(View=‘’ErrorPage’’, ExceptionType=typeof(SqlException)]<br />public ActionResult ProductList() {<br /> return View();<br />}<br />
  23. 23. OutputCache Filter<br />
  24. 24. Handling Unknown Actions<br />public class HomeController : Controller<br />{<br /> protected override void HandleUnknownAction(string actionName)<br /> {<br /> .....<br /> }<br />}<br />

×