Implement your own controller
Normally defined controllers inherit from the controller class, and the controller class inherits Controllerbase, which finally implements the IController interface.
Public Interface IController { void Execute (RequestContext requestcontext); }
Implement your own controller
Create a new class in the Controller folder named Minecontroller, let's implement the IController interface
Public void Execute (System.Web.Routing.RequestContext requestcontext) { = requestContext.HttpContext.Timestamp; string action = requestcontext.routedata.values["action"]. ToString (); RequestContext.HttpContext.Response.Write (string. Format (" 0},action name{1}", time. ToString (), action)); }
Enter Http://localhost:8398/Mine/Index in the browser
Successful implementation of one of your own handwritten controller controllers
Of course, when implementing the controller, we still inherit the controllers abstract class
The interaction of data
Controller three ways to get the data are mainly 1. A series of context objects 2. Parameters passed to the action Method 3. Model binding feature for the calling framework that is displayed
Context objects: Request, Response, Routedata, HttpContext, and Server
The Controller abstract class encapsulates the primary context object
Public Get ; } Public Get ; } Public Get ; } Public Get ; } Public get; }
Of course we can get the value as request.form,request.querystring as in WebForm.
Arguments passed to the action
In the first method we get the data by getting the contextual information, encapsulating the more powerful way to get the information in MVC, and we can get its value by passing in the parameter in the action's method.
DateTime time =string action = requestcontext.routedata.values["action" ]. ToString ();
Through the encapsulation of MVC, we can rewrite
Public ActionResult Index (datatime time,string action) {}
And this neat way doesn't have to be the case.
Why you can pass the value in this way:
The Controller class obtains the value of the parameter for the Action method through the value provider of the MVC framework and the model binder component.
Value provider provides a range of values that can be accessed in the controller, internally it passes from Request.Form, Request.QueryString, Request.Files, and Routedata.values Data (a collection of key values), and then passing the data to model Binder,model Binder attempts to match the data to the parameters of the action method. The default model binder can be created and assigned to any. NET type Object parameters (that is, the parameters of the Action method), including the collection and the custom type.
Realize your actionresult.
There are a lot of actionresult defined in the controller, so let's do our own actionresult, let's see what's in the abstract class ActionResult.
Public Abstract class ActionResult { protected actionresult (); Public Abstract void Executeresult (ControllerContext context); }
Seems to implement Actionrsult, after inheritance is mainly to rewrite the Executeresult method, we create a new class Myactionresult
Public class Myactionresult:actionresult { publicoverridevoid Executeresult (controllercontext context) { string Controller = context. routedata.values["controller"]. ToString (); Context. HttpContext.Response.Write (string. Format (" director name {0}", Controller)); } }
In the controller we create a new ActionResult
Public actionresult Testmycontroller () { returnnew myactionresult (); }
Enter Http://localhost:8398/Login/testMyController in the browser
We can see that our custom ActionResult has been successfully implemented.
Action passes data to the view view
Here are a few ways to pass data from the view view to action, and now summarize some of the ways in which you can pass data to the view
ViewModel Object
Public actionresult Testmycontroller () { = datetime.now; return View (time); }
@model datetime@{ "testmycontroller";} is @Model .dayofweek</span>
The role of @Model in the foreground is to declare the type of the model property, eliminating the hassle of type conversion, and @Model is a reference to the view model object.
There are several ways to ViewBag, ViewData, and TempData properties
ASP. NET MVC Controller Learning 2