controllersactions-110711131108-phpapp01

Upload: ameshlal

Post on 05-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 controllersactions-110711131108-phpapp01

    1/19

    Controllers &

    Actions in MVC

    Eyal VardiCEO E4D Solutions LTDMicrosoft MVP Visual C#

    blog: www.eVardi.com

  • 7/31/2019 controllersactions-110711131108-phpapp01

    2/19

    2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    Agenda

    Controllers Overview

    ViewData vs. ViewBag

    Action & ActionResult

    Controller Attributes

    Async Controller

  • 7/31/2019 controllersactions-110711131108-phpapp01

    3/19

    2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    Understanding Controllers

    MVC controllers are responsible forresponding to requests made against anASP.NET MVC website.

  • 7/31/2019 controllersactions-110711131108-phpapp01

    4/19

    2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    The Controller Responsibility

    Execute the right action method andvalidating that it can be called.

    Getting the values to use as the action

    method's arguments.

    Handling all errors that might occur during theexecution of the action method.

    Providing the default WebFormViewEngineclass for rendering ASP.NET page types(views).

  • 7/31/2019 controllersactions-110711131108-phpapp01

    5/19

    2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    Controller Sample

    http://E4D.co.il / Course / Net / MVC / http://E4D.co.il / Course/ Net ? name=MVC

    publicclassCourseController : Controller

    {

    publicActionResult Net( string name )

    {

    var course = BL.GetCourse(name);

    return View( course );}

    }

  • 7/31/2019 controllersactions-110711131108-phpapp01

    6/19

    2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    ViewData Property

    Used to set view-specific data in adictionary object that can hold multiplename/value pairs.

    publicclassCourseController : Controller

    {

    publicActionResult Net( string name )

    {

    ViewData["Course"] = BL.GetCourse(name);

    return View();}

    }

  • 7/31/2019 controllersactions-110711131108-phpapp01

    7/19 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    ViewBag Property

    Used to set view-specific data in adictionary object that can hold multiplename/value pairs.

    publicclassCourseController : Controller

    {

    publicActionResult Net( string name )

    {

    ViewBag.Course = BL.GetCourse(name);

    return View();}

    }

  • 7/31/2019 controllersactions-110711131108-phpapp01

    8/19 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    Actions

    Action == Method

    Action must meet certain requirements:

    Must be public.

    Cannot be a static method.

    Cannot be an extension method.

    Cannot have open generic types.

    The method is not a method of the controller base class.

    The method cannot contain ref or out parameters.

  • 7/31/2019 controllersactions-110711131108-phpapp01

    9/19

  • 7/31/2019 controllersactions-110711131108-phpapp01

    10/19 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    Action Results

    A controller action returnssomething called an action result.

    Action results types:

    ViewResult

    EmptyResult

    RedirectResult

    JsonResult

    JavaScriptResult

    ContentResult

    FileContentResult

    FilePathResult

    FileStreamResult

    HttpNotFoundResult

    HttpRedirectResult

    HttpStatusCodeResult

  • 7/31/2019 controllersactions-110711131108-phpapp01

    11/19 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    Controller View Methods

    Normally, you do not

    return an action result

    directly. Instead, youcall one of the following

    methods of the

    Controller base class.

  • 7/31/2019 controllersactions-110711131108-phpapp01

    12/19 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    Controller ViewMethods

  • 7/31/2019 controllersactions-110711131108-phpapp01

    13/19 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    Controller Class

  • 7/31/2019 controllersactions-110711131108-phpapp01

    14/19 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    Controller Attributes

    [ActionName]

    [NonAction]

    [AcceptVerbs]

    [HttpGet]

    [HttpPost]

    [HttpPut]

    [HttpDelete]

    [ChildActionOnly]

    [SessionState]

    [OutputCache]

  • 7/31/2019 controllersactions-110711131108-phpapp01

    15/19 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    SessionState Attribute

    Using the attribute, you can completely turnon or off session state, adjust it to read-only,or make it required. Default, Required, ReadOnly & Disabled.

    [SessionState( SessionStateBehavior.Disabled )]

    publicclassHomeController : Controller

    {

    publicActionResult Index()

    {

    Session["E4D"] = "E4D Learning";

    return View();

    }

    }

  • 7/31/2019 controllersactions-110711131108-phpapp01

    16/19 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    OutputCache Attribute

    [ChildActionOnly][OutputCache( Duration = 20 )]

    publicActionResult CurrentTime()

    {

    return PartialView();

    }

    OutputCache Sample

    View Time: @DateTime.Now.ToString("h:mm:ss")

    Partial View Time:

    @{

    Html.RenderAction("CurrentTime");

    }

  • 7/31/2019 controllersactions-110711131108-phpapp01

    17/19 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    Async Controller

    The AsyncControllerclass enables you towrite asynchronous action methods.

  • 7/31/2019 controllersactions-110711131108-phpapp01

    18/19 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

    Async Action

    publicclassPortalController : AsyncController{

    [AsyncTimeout(Duration = 5000 )]

    publicvoid NewsAsync(string city)

    {

    AsyncManager.OutstandingOperations.Increment();

    NewsService newsService = new NewsService();

    newsService.GetHeadlinesCompleted += (sender, e) =>

    {

    AsyncManager.Parameters["headlines"] = e.Value;

    AsyncManager.OutstandingOperations.Decrement();

    };

    newsService.GetHeadlinesAsync(city);

    }publicActionResult NewsCompleted(NewsHeadline[] headlines)

    {

    return View( "News",

    newViewStringModel { Headlines = headlines } );

    }

    }

    Async Action

  • 7/31/2019 controllersactions-110711131108-phpapp01

    19/19 2010 E4D LTD All i h d T l 054 5 767 300 E il E l@E4D il

    Async Action