• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Handle requests with controllers in ASP.NET Core MVC(转发)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

原文:https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/actions?view=aspnetcore-6.0

 

Controllers, actions, and action results are a fundamental part of how developers build apps using ASP.NET Core MVC.

What is a Controller?

A controller is used to define and group a set of actions. An action (or action method) is a method on a controller which handles requests. Controllers logically group similar actions together. This aggregation of actions allows common sets of rules, such as routing, caching, and authorization, to be applied collectively. Requests are mapped to actions through routing.

 

What is a Controller?

A controller is used to define and group a set of actions. An action (or action method) is a method on a controller which handles requests. Controllers logically group similar actions together. This aggregation of actions allows common sets of rules, such as routing, caching, and authorization, to be applied collectively. Requests are mapped to actions through routing.

By convention, controller classes:

  • Reside in the project's root-level Controllers folder.
  • Inherit from Microsoft.AspNetCore.Mvc.Controller.

A controller is an instantiable class, usually public, in which at least one of the following conditions is true:

  • The class name is suffixed with Controller.
  • The class inherits from a class whose name is suffixed with Controller.
  • The [Controller] attribute is applied to the class.

A controller class must not have an associated [NonController] attribute.

 

Controllers should follow the Explicit Dependencies Principle. There are a couple of approaches to implementing this principle. If multiple controller actions require the same service, consider using constructor injection to request those dependencies. If the service is needed by only a single action method, consider using Action Injection to request the dependency.

Within the Model-View-Controller pattern, a controller is responsible for the initial processing of the request and instantiation of the model. Generally, business decisions should be performed within the model.

The controller takes the result of the model's processing (if any) and returns either the proper view and its associated view data or the result of the API call. Learn more at Overview of ASP.NET Core MVC and Get started with ASP.NET Core MVC and Visual Studio.

The controller is a UI-level abstraction. Its responsibilities are to ensure request data is valid and to choose which view (or result for an API) should be returned. In well-factored apps, it doesn't directly include data access or business logic. Instead, the controller delegates to services handling these responsibilities.

 

Defining Actions

Public methods on a controller, except those with the [NonAction] attribute, are actions. Parameters on actions are bound to request data and are validated using model binding. Model validation occurs for everything that's model-bound. The ModelState.IsValid property value indicates whether model binding and validation succeeded.

Action methods should contain logic for mapping a request to a business concern. Business concerns should typically be represented as services that the controller accesses through dependency injection. Actions then map the result of the business action to an application state.

Actions can return anything, but frequently return an instance of IActionResult (or Task<IActionResult> for async methods) that produces a response. The action method is responsible for choosing what kind of response. The action result does the responding.

 

Controller Helper Methods

Controllers usually inherit from Controller, although this isn't required. Deriving from Controller provides access to three categories of helper methods:

1. Methods resulting in an empty response body

No Content-Type HTTP response header is included, since the response body lacks content to describe.

There are two result types within this category: Redirect and HTTP Status Code.

  • HTTP Status Code

    This type returns an HTTP status code. A couple of helper methods of this type are BadRequestNotFound, and Ok. For example, return BadRequest(); produces a 400 status code when executed. When methods such as BadRequestNotFound, and Ok are overloaded, they no longer qualify as HTTP Status Code responders, since content negotiation is taking place.

  • Redirect

    This type returns a redirect to an action or destination (using RedirectLocalRedirectRedirectToAction, or RedirectToRoute). For example, return RedirectToAction("Complete", new {id = 123}); redirects to Complete, passing an anonymous object.

    The Redirect result type differs from the HTTP Status Code type primarily in the addition of a Location HTTP response header.

 

2. Methods resulting in a non-empty response body with a predefined content type

Most helper methods in this category include a ContentType property, allowing you to set the Content-Type response header to describe the response body.

There are two result types within this category: View and Formatted Response.

  • View

    This type returns a view which uses a model to render HTML. For example, return View(customer); passes a model to the view for data-binding.

  • Formatted Response

    This type returns JSON or a similar data exchange format to represent an object in a specific manner. For example, return Json(customer); serializes the provided object into JSON format.

    Other common methods of this type include File and PhysicalFile. For example, return PhysicalFile(customerFilePath, "text/xml"); returns PhysicalFileResult.

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap