Controllers & Actions in MVC<br />Eyal Vardi<br />CEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.eVardi.com<br />
Agenda<br />Controllers Overview<br />ViewData vs. ViewBag<br />Action & ActionResult<br />Controller Attributes<br />Asyn...
Understanding Controllers<br />MVC controllers are responsible for responding to requests made against an ASP.NET MVC webs...
The Controller  Responsibility<br />Execute the right action method and validating that it can be called.<br />Getting the...
Controller Sample<br />http://E4D.co.il / Course / Net / MVC /<br />http://E4D.co.il / Course/ Net ? name=MVC<br />public ...
ViewData Property<br />Used to set view-specific data in a dictionary object that can hold multiple name/value pairs.<br /...
ViewBag Property<br />Used to set view-specific data in a dictionary object that can hold multiple name/value pairs.<br />...
Actions<br />Action == Method<br />Action must meet certain requirements:<br />Must be public.<br />Cannot be a static met...
Preventing an Action to Invoke<br />Prevent the method from being invoked by using the [NonAction] attribute. <br />public...
Action Results<br />A controller action returns something called an action result.<br />Action results types:<br />ViewRes...
Controller “View” Methods<br />Normally, you do not return an action result directly. Instead, you call one of the followi...
Controller “View”Methods<br />
Controller Class<br />
Controller Attributes<br />[ChildActionOnly]<br />[SessionState]<br />[OutputCache]<br />[ActionName]<br />[NonAction]<br ...
SessionState Attribute<br />Using the attribute, you can completely turn on or off session state, adjust it to read-only, ...
OutputCacheAttribute<br />[ChildActionOnly]<br />[OutputCache( Duration = 20 )]<br />public ActionResult CurrentTime()    ...
Async Controller<br />The AsyncControllerclass enables you to write asynchronous action methods.<br />
Async Action<br />public class PortalController : AsyncController{<br />     [AsyncTimeout(Duration = 5000 )]<br />    pub...
Async Action<br />
Upcoming SlideShare
Loading in …5
×

Controllers & actions

1,875 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,875
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
95
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • http://davidhayden.com/blog/dave/archive/2011/02/09/SessionLessControllersMvc3.aspx
  • http://davidhayden.com/blog/dave/archive/2011/01/25/PartialPageOutputCachingASPNETMVC3.aspx
  • Controllers & actions

    1. 1. Controllers & Actions in MVC<br />Eyal Vardi<br />CEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.eVardi.com<br />
    2. 2. Agenda<br />Controllers Overview<br />ViewData vs. ViewBag<br />Action & ActionResult<br />Controller Attributes<br />Async Controller<br />
    3. 3. Understanding Controllers<br />MVC controllers are responsible for responding to requests made against an ASP.NET MVC website.<br />
    4. 4. The Controller Responsibility<br />Execute the right action method and validating that it can be called.<br />Getting the values to use as the action method's arguments.<br />Handling all errors that might occur during the execution of the action method.<br />Providing the default WebFormViewEngineclass for rendering ASP.NET page types (views).<br />
    5. 5. Controller Sample<br />http://E4D.co.il / Course / Net / MVC /<br />http://E4D.co.il / Course/ Net ? name=MVC<br />public class CourseController : Controller{   public ActionResult Net( string name )   {       var course = BL.GetCourse(name);       return View( course );   }<br />}<br />
    6. 6. ViewData Property<br />Used to set view-specific data in a dictionary object that can hold multiple name/value pairs.<br />public class CourseController : Controller{   public ActionResult Net( string name )   {       ViewData["Course"] = BL.GetCourse(name);       return View();   }<br />}<br />
    7. 7. ViewBag Property<br />Used to set view-specific data in a dictionary object that can hold multiple name/value pairs.<br />public class CourseController : Controller{   public ActionResult Net( string name )   {       ViewBag.Course = BL.GetCourse(name);       return View();   }<br />}<br />
    8. 8. Actions<br />Action == Method<br />Action must meet certain requirements:<br />Must be public.<br />Cannot be a static method.<br />Cannot be an extension method. <br />Cannot have open generic types.<br />The method is not a method of the controller base class.<br />The method cannot contain ref or out parameters.<br />
    9. 9. Preventing an Action to Invoke<br />Prevent the method from being invoked by using the [NonAction] attribute. <br />public class HomeController : Controller{    [NonAction]    public string CompanySecrets()    {        return "This information is secret.";    }<br /> ...}<br />
    10. 10. Action Results<br />A controller action returns something called an action result.<br />Action results types:<br />ViewResult<br />EmptyResult<br />RedirectResult<br />JsonResult<br />JavaScriptResult<br />ContentResult<br />FileContentResult<br />FilePathResult<br />FileStreamResult<br />HttpNotFoundResult<br />HttpRedirectResult<br />HttpStatusCodeResult<br />
    11. 11. Controller “View” Methods<br />Normally, you do not return an action result directly. Instead, you call one of the following methods of the Controller base class.<br />
    12. 12. Controller “View”Methods<br />
    13. 13. Controller Class<br />
    14. 14. Controller Attributes<br />[ChildActionOnly]<br />[SessionState]<br />[OutputCache]<br />[ActionName]<br />[NonAction]<br />[AcceptVerbs]<br />[HttpGet]<br />[HttpPost]<br />[HttpPut]<br />[HttpDelete]<br />
    15. 15. SessionState Attribute<br />Using the attribute, you can completely turn on or off session state, adjust it to read-only, or make it required.<br />Default,  Required,  ReadOnly & Disabled.<br />[SessionState( SessionStateBehavior.Disabled )]<br />public class HomeController : Controller     <br />{<br />   public ActionResult Index()<br />   {<br />       Session["E4D"] = "E4D Learning";             <br /> return View();<br />   }     <br />}<br />
    16. 16. OutputCacheAttribute<br />[ChildActionOnly]<br />[OutputCache( Duration = 20 )]<br />public ActionResult CurrentTime()         <br />{<br />    return PartialView();         <br />}<br /><h2>OutputCache Sample</h2> View Time: @DateTime.Now.ToString("h:mm:ss")<br /> Partial View Time: <br />@{<br /> Html.RenderAction("CurrentTime");<br />}<br />
    17. 17. Async Controller<br />The AsyncControllerclass enables you to write asynchronous action methods.<br />
    18. 18. Async Action<br />public class PortalController : AsyncController{<br /> [AsyncTimeout(Duration = 5000 )]<br /> publicvoidNewsAsync(string city) <br /> { <br />AsyncManager.OutstandingOperations.Increment(); <br />NewsServicenewsService = newNewsService(); <br />newsService.GetHeadlinesCompleted+= (sender, e) => <br /> { <br />AsyncManager.Parameters["headlines"] = e.Value; <br />AsyncManager.OutstandingOperations.Decrement(); <br /> }; <br />newsService.GetHeadlinesAsync(city); <br /> } <br /> public ActionResult NewsCompleted(NewsHeadline[] headlines) {    return View( "News",                  new ViewStringModel { Headlines = headlines } );<br /> }}<br />Async Action<br />
    19. 19. Async Action<br />

    ×