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();
}