Controller is the core module of ASP. NET MVC. It is the regulator between view and model, which embodies the business logic of application program. By introducing the method of ASP.NET MVC controller creating custom controller through IController interface and facilitating built-in controller by inheriting Controller class, this paper elaborates respectively from request receiving input to response output, in order to show the charm of ASP.NET MVC controller. ASP.NET is a new technology released by Microsoft Corp in March 2002. This technology can only be used to replace ASP technology for Web development. At that time, it was still the world of C/S architecture applications, so ASP.NET created the design idea of Web Form in order to enable C/S architecture programmers to quickly start Web development. With the development of Web development, some new ideas of software development and the rapid development of Web front-end technology, especially the rise and development of AJAX technology, make Web Form develop modern Web application more and more powerless. Many B/S programmers released a breathtaking ASP.NET MVC framework in 2009 when they criticized how bad it was to use Web Form development program. The ASP.NET MVC framework is not designed to replace Web Form, but to provide developers with another choice to develop B/S architecture applications.
MVC is a pattern for designing and creating Web applications using MVC (Model View Controller Model-View Controller Controller Model-Controller). [1] MVC framework model was proposed by Xerox PARC of Smalltalk project group in 1978. ASP.NET MVC also enhances separation of concerns based on the implementation of MVC framework pattern. ASP.NET MVC can fully fulfill any requirement that WebForm can fulfill.
Conversion to ASP.NET MVC is a natural process for ASP.NET developers. The main advantages of ASP.NET MVC are: good testability, powerful routing function, using the most advanced. NET framework for development, building on a stable and fully verified ASP.NET platform, and ASP.NET MVC has been source. With the support of such top-level software companies, we can fully anticipate the good development of ASP.NET MVC. ASP.NET MVC is Model-View-Controller mode, but the core is controller. The controller is responsible for processing the request, processing the data, then responding to the data as a view, and finally generating HTML. Controllers act as intermediaries between user requests and server-side operations. Controllers connect user interfaces and arrange how pages are fed back to users.
Controllers only control the feedback of pages. The specific presentation of pages is not the work of controllers. That’s what views should do. The main function of the controller is to encapsulate the business logic of the Web application. In ASP.NET MVC framework, the controller class must implement the System.
Web.Mvc.IController interface, which has only one Execute method to implement. The ASP.NET MVC framework is freely customizable and can be completed by implementing the IController interface. It is recommended to build the System.Web.Mvc.Controller by inheriting the provided System.Web.Mvc.Controller.
Controller class provides three features: action method, action result and filter, which make our development more efficient, program running more stable and code writing more elegant. The controller is mainly responsible for receiving requests and outputting results. Controllers often need to accept request input by querying strings, submitting form values, and URL parsing parameters of the routing system. These data are accessed mainly through three ways: context object extraction, action method parameter acquisition and model Binding. When a developer inherits the Controller class to build a controller, he will get some Request, Response, HttpContext and Server attributes, which can be accessed through the Controller Context attribute.
These attributes contain specific request information. It is more intuitive, convenient, easy to read and unit test to accept input through the parameters of action method than the way to get data from context. Action method parameters do not allow out and ref parameters, otherwise an exception will be thrown. The parameter assignment of action method is accomplished by the context objects Request. QueryString, Request.
Form and RouteData. Values. The Controller class uses the built-in Value Provider and Model Binder of ASP.NET MVC to get the parameter values of the action method. Value Provider grabs data items from Request. QueryString, Request. Form, and RouteData.
Values and passes these values to Model Binder, which maps these data into data types of action method parameters. For the parameters of the action method, the value type parameter is mandatory, and the reference type parameter is optional. If the action method contains a parameter value that cannot be converted to the correct type, ASP.NET MVC passes the default value of the parameter type and registers the value as a validation error in ModelState. Without checking for ModelState validation errors, if the user enters bad data in the form, the request will be processed with a result without input data or input default values. The controller receives a response after processing the request. When implemented through the IController interface, you need to be responsible for handling all aspects of the request, even generating responses to the client. You can also return the HttpResponseBase class in the Execute method by inheriting Controller. When an action method returns an action result object, ASP. NET MVC calls the ExecuteResult method of the object, then processes the Response object in the action result, and finally generates the desired output. ASP.NET MVC processes the action result by returning an action result object from the action method. When ASP.NET MVC receives the action result object, it calls the corresponding action result class according to the type of the object. This is the ExecuteResult method that executes the action result. The ExecuteResult method handles the Response object and generates the desired output result. ASP.NET MVC has built-in many action result types inherited from the ActionResult class. The most common form of response in action methods is HTML, which can be accomplished by an instance of the ViewResult class. Another form of response is to redirect to another URL instead of producing output directly, which can be done by an instance of the RedirectResult class. Redirect to a literal URL. Redirect method can be used to redirect to HTTP code 302 (temporary redirection), or Redirect Permanent method can be used to redirect to HTTP code 301 (permanent redirection). Redirect to the URL of the routing system. It can be implemented by RedirectToRoute method, which has the advantage that when the routing system is modified, the response URL will be updated. Redirect to action method. RedirectToAction can be achieved by RedirectToAction and RedirectToAction Permanent methods, RedirectToAction redirects to HTTP code 302 (temporary redirection), RedirectToAction Permanent redirects to HTTP code 301 (permanent redirection). Redirect the transfer of data. Because redirection means cross-domain requests, thermostatic element ViewBag cannot complete cross-domain requests, so the transfer of data in redirection needs to be done using TempData. Its usage is similar to that of Session, except that TempData is marked as deleted after being read and deleted after request processing is completed. The last form of response is HTTP code. HTTP result code can be returned by HttpStatusCodeResult method, HTTP code 404 can be returned by HttpNotFound method (the page visited does not exist), and HTTP code 401 can be returned by HttpUnauthorized Result method (unauthorized request). Controller is the core module of ASP.
NET MVC. It is the regulator between view and model, which embodies the business logic of application program. This paper introduces the method of ASP.NET MVC controller to create custom controller by implementing IController interface and to facilitate built-in controller by inheriting Controller class. From request receiving input to response output, this paper elaborates respectively, in order to show the charm of ASP.NET MVC controller.