Asp.net mvc (7)-asp.net mvc 3.0 Controller

Source: Internet
Author: User

[Index page]
[Download source code]

Asp.net mvc (7)-asp.net mvc 3.0 Controller

Author: webabcd

Introduction
Asp.net mvc the new features of asp.net mvc 3.0 Controller:

  • Global Action Filter
  • You can use OutputCache on the Action marked as ChildActionOnly.
  • ViewBag
  • Added some Action results.

Example
1. Demo of Global Action Filter
Global. asax. cs (register Global Action Filter)

Protected void Application_Start () {AreaRegistration. registerAllAreas (); RegisterRoutes (RouteTable. routes);/** demonstrate Global Action Filter * // instantiate a Filter var handleError = new HandleErrorAttribute (); // specify the View handleError of HandleErrorAttribute. view = "Error2"; // The default value of the Order attribute is-1, which is not applied. Therefore, modify handleError here. order = 0; // Add the Filter object to GlobalFilters in the global Filters set. filters. add (handleError );}

 

Web. config

<System. web> <! -- To enable HandleError. <customErrors mode = "On"/> --> <customErrors mode = "On"/> </system. web>

 

ControllerDemoController. cs

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; namespace MVC30.Controllers {public class ControllerDemoController: Controller {// used to demonstrate Global Action Filter public ActionResult GlobalActionFilter () {throw new Exception ("exception ");}}}

 

GlobalActionFilter. cshtml (an exception is thrown when you access this page, and then jump to Error2)

@{    ViewBag.Title = "Global Action Filter";}

 

Error2.cshtml (custom error page)

@ {Layout = null ;}<! DOCTYPE html> 

 

2. The Action marked as ChildActionOnly supports OutputCache.
ControllerDemoController. cs

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; namespace MVC30.Controllers {public class ControllerDemoController: Controller {public ActionResult ChildActionOnlyDemo () {return View () ;}// ChildActionOnly-specifies that Action can only be called by RenderAction // OutputCache () -cache. Duration-Cache seconds. VaryByParam-none, *. Multiple parameters are separated by commas. You can also set [ChildActionOnly] [OutputCache (Duration = 5)] public PartialViewResult _ GetCurrentTime () {var currentTime = DateTime through the configuration file. now; return PartialView (currentTime );}}}

 

_ GetCurrentTime. cshtml

@ * Specify the model type through @ Model, and the Model object is the data returned by the Action * @ model DateTime <div> currentTime: @ Model. toString ("yyyy-MM-dd HH: mm: ss") </div>

 

ChildActionOnlyDemo. cshtml

@ {ViewBag. title = "OutputCache can be used on actions marked as ChildActionOnly";} 

 

3. Added ViewBag.
ControllerDemoController. cs

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; namespace MVC30.Controllers {public class ControllerDemoController: Controller {// the essence of ViewBagDemo public ActionResult ViewBagDemo () {// ViewBag is to package ViewData into a dynamic ViewBag. message = "ViewBag Demo"; return View ();}}}

 

ViewBagDemo. cshtml

@{    ViewBag.Title = "ViewBag";}

 

4. Newly Added Action Result

<P> some Action results are added to the Controller: HttpNotFoundResult, HttpRedirectResult, and HttpStatusCodeResult. </p>

 

OK
[Download source code]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.