ASP.NET Core MVC's Controller (Controller)

  Operation (action) and the operation result (action result) is a fundamental part of ASP.NET MVC building applications.

  In ASP.NET MVC, and the polymerization controller used to define a set of operations. A method of operation of the controller is processing the incoming requests. The controller provides a logical way, similar operation will be organized, allowing some general rules (e.g., routing, caching, authorized) use a common application. Incoming request by  routing  maps to operation.

  ASP.NET Core MVC, the controller may be in any "Controller" inherited from the end or end with "Controller" class can be instantiated. The controller should follow the principle shown to be dependent, and acquires its operation requires dependency by using the constructor dependency injection.

  By convention, the controller class:

    "Controller" file located in the root directory folder.

    Inherited from Microsoft.AspNetCore.Mvc.Conrroller 

  These two conventions are not required.

 

  In the MVC pattern, the controller is responsible for initializing and requests instantiation model. Generally speaking, the business logic should be placed in the model execution.

  Models should be the common CLR objects, database rather than DbContext or related types.

  Model controller acquires the processing result (if any), and returns the appropriate view and a view associated data.

  The controller is a UI-level abstraction. Its role is to ensure that incoming request data is valid, and select which view (or the results of the API) should return. Generally in the controller does not contain direct data access or business logic, but service commission to handle these tasks.

 

1. Definitions operation (Action)

  Any common method on the controller is operating. Operational model parameters using the binding request and verified.

  The method of operating parameters should verify ModelState.IsValid accepts attribute to true.

  The method comprises the logic operation should be mapped to service incoming requests concern. Business concern is usually expressed as a controller injection services accessible through dependence. Then, the operation of the mapping result to the operational state of the application.

  Operation may return any content, but the examples are typically generated in response to IActionResult return. Operation is responsible for what kind of response options, the results of operations responsible response.

 

2. The controller-assisted method

  Although not required, generally inherit their controller from the Controller base class. Resulting controller can access a lot of useful properties and methods.

3. View View

  Returns scent of ink render HTML views, such as, return View (model);

4.HTTP status code

  Returns an HTTP status code, such as, renturn BadRequest ();

5. Format Response

  Return JSON format or similar objects in a particular manner. Such as, return Json (model);

6. Content negotiation response

  Operation may return the response content negotiation (with OK, Created, CreateAtRoute or CreateAtAction), rather than returning to the object.

7. Redirection

  The operation returns to another target or redirection (using Redirect, LocalRedirect, RedirectToAction or RedirectToRoute).

 

  In addition to the above method, a simple operation can also return an object. In this case, the object will be formatted according to client requests.

  In most applications, many operations workflow section is shared. For example, most applications can only authenticated users, or to take advantage of caching. When it is desired to perform certain logic operation before or after the method of operation, the filter may be used. These may be processed through the filter crosscutting concerns, to avoid operation becomes cumbersome.

  In the case of authorization and authentication, may be "Authorize" characteristic of applications requiring any controller or its operation.

  In an MVC application, many crosscutting concerns can use filters to deal with. Alternatively middleware.

 

Guess you like

Origin www.cnblogs.com/afei-24/p/11330710.html