controllers & actions
TRANSCRIPT
Controllers & Actions in MVC
Eyal VardiCEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.eVardi.com
© 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
© 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.
© 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).
© 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 ); }}
© 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(); }}
© 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(); }}
© 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.
© 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."; } ...}
© 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
© 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.
© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Controller “View”Methods
© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Controller Class
© 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]
© 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(); } }
© 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"); }
© 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.
© 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
© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Async Action