asp.net MVC Framework Experience (4): Controller

Source: Internet
Author: User

Overview

In MVC, controller is used to process and respond to the user's interactions, choose which view to use to display, what views data needs to be passed to the view, and so on. The ASP.net MVC framework provides two types of IController interfaces and controller base classes, where controller provides some common processing in MVC, such as locating the correct action and executing, Assigns values to action method parameters, handles errors during execution, and provides a default Webformviewfactory rendering page. IController only provides a controller interface, if the user wants to customize a controller, you can implement IController, which is defined as follows:

public interface IController
{
  void Execute(ControllerContext controllerContext);
}

Defining Controllers and action

In the previous three examples, we have defined the controller as long as it inherits from the controller:

public class BlogController : Controller
{
  [ControllerAction]
  public void Index()
  {
    BlogRepository repository = new BlogRepository();
    List<Post> posts = repository.GetAll();
    RenderView("Index", posts);
  }
   [ControllerAction]
  public void New()
  {
     RenderView("New");
  }
}
通过ControllerAction特性来指 定一个方法为action,ControllerAction的定义非常简单:

[AttributeUsage (AttributeTargets.Method)]
public sealed class ControllerActionAttribute : Attribute
{
   public ControllerActionAttribute();
}

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.