controllers & actions

19
Controllers & Actions in MVC Eyal Vardi CEO E4D Solutions LTD Microsoft MVP Visual C# blog: www.eVardi.com

Upload: eyal-vardi

Post on 17-May-2015

1.569 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Controllers & actions

Controllers & Actions in MVC

Eyal VardiCEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.eVardi.com

Page 2: Controllers & actions

© 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

Page 3: Controllers & actions

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

Understanding Controllers

MVC controllers are responsible for responding to requests made against an ASP.NET MVC website.

Page 4: Controllers & actions

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

The Controller Responsibility

Execute the right action method and validating that it can be called.

Getting the values to use as the action method's arguments.

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

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

Page 5: Controllers & actions

© 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

public class CourseController : Controller{   public ActionResult Net( string name )   {       var course = BL.GetCourse(name);       return View( course );   }}

Page 6: Controllers & actions

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

ViewData Property

Used to set view-specific data in a dictionary object that can hold multiple name/value pairs.

public class CourseController : Controller{   public ActionResult Net( string name )   {       ViewData["Course"] = BL.GetCourse(name);       return View();   }}

Page 7: Controllers & actions

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

ViewBag Property

Used to set view-specific data in a dictionary object that can hold multiple name/value pairs.

public class CourseController : Controller{   public ActionResult Net( string name )   {       ViewBag.Course = BL.GetCourse(name);       return View();   }}

Page 8: Controllers & actions

© 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.

Page 9: Controllers & actions

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

Preventing an Action to Invoke

Prevent the method from being invoked by using the [NonAction] attribute.

public class HomeController : Controller{    [NonAction]    public string CompanySecrets()    {        return "This information is secret.";    } ...}

Page 10: Controllers & actions

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

Action Results

A controller action returns something called an action result.

Action results types:

ViewResult

EmptyResult

RedirectResult

JsonResult

JavaScriptResult

ContentResult

FileContentResult

FilePathResult

FileStreamResult

HttpNotFoundResult

HttpRedirectResult

HttpStatusCodeResult

Page 11: Controllers & actions

© 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, you call

one of the following

methods of the

Controller base class.

Page 12: Controllers & actions

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

Controller “View”Methods

Page 13: Controllers & actions

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

Controller Class

Page 14: Controllers & actions

© 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]

Page 15: Controllers & actions

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

SessionState Attribute

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

[SessionState( SessionStateBehavior.Disabled )]public class HomeController : Controller     {   public ActionResult Index()   {       Session["E4D"] = "E4D Learning";              return View();   }     }

Page 16: Controllers & actions

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

OutputCache Attribute[ChildActionOnly][OutputCache( Duration = 20 )]public ActionResult CurrentTime()         {    return PartialView();         }

<h2>OutputCache Sample</h2> View Time: @DateTime.Now.ToString("h:mm:ss")<br /> Partial View Time: @{ Html.RenderAction("CurrentTime"); }

Page 17: Controllers & actions

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

Async Controller

The AsyncController class enables you to write asynchronous action methods.

Page 18: Controllers & actions

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

Async Action

public class PortalController : AsyncController{ [AsyncTimeout(Duration = 5000 )] public void 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); } public ActionResult NewsCompleted(NewsHeadline[] headlines) {     return View( "News",                   new ViewStringModel { Headlines = headlines } ); }}

Async Action

Page 19: Controllers & actions

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

Async Action