- Hands-On Full:Stack Web Development with ASP.NET Core
- Tamir Dresher Amir Zuker Shay Friedman
- 320字
- 2021-06-10 19:37:32
IActionResult and ActionResult<T>
The IActionResult interface defines the contract <indexentry content="HTTP response:ActionResult"> for action method responses. The default implementation of the interface is provided by the ActionResult and ActionResult<T> types, but ASP.NET Core provides many derived response types that you can use to fully control the HTTP response that will be generated. Here is a short list of some of them:
- EmptyResult: Generates an HTTP response with a success status code (200 OK) and an empty body.
- ObjectResult: Generates a response with a body, where the body is a serialization of an object you provide. The object is serialized to the content type that was requested by the client.
- BadRequestResult: Generates a response with a BadRequst status code (400) that signals to the client that the given request is invalid.
- ViewResult: Allows you to render a view, from a cshtml file, for example, and return the result (as HTML) to the client.
- FileResult: Generates a response that contains file data. There are derived classes for various file reading formats.
- RedirectResult: Generates a response with a temporary redirect (302) or permanent redirect status code (301) that contains the URL to navigate to.
Starting from ASP.NET Core 2.1, the ActionResult<T> type can be used as the return type of a controller action to make it explicit what the type of the body will be in a successful response, while still allowing for the return of other response types if needed.
For example, if GiveNTake receives a request to add a product with invalid request data, we send the client a BadRequestResult, and if the request was valid, we echo the request data with an ObjectResult, like this:
[HttpPost("")]
public ActionResult<NewProductDTO> AddNewProduct([FromBody] NewProductDTO newProduct)
{
if (!ModelState.IsValid)
{
return new BadRequestResult();//(ModelState);
}
return newProduct;
}
- 廣電5G從入門到精通
- Hands-On Industrial Internet of Things
- 局域網(wǎng)組建、管理與維護項目教程(Windows Server 2003)
- 企業(yè)網(wǎng)絡(luò)安全管理
- 通信原理及MATLAB/Simulink仿真
- 工業(yè)互聯(lián)網(wǎng)創(chuàng)新實踐
- Android UI Design
- Practical Web Penetration Testing
- 基于IPv6的家居物聯(lián)網(wǎng)開發(fā)與應(yīng)用技術(shù)
- 圖神經(jīng)網(wǎng)絡(luò)前沿
- Intelligent Mobile Projects with TensorFlow
- 物聯(lián)網(wǎng)商業(yè)設(shè)計與案例
- CDN技術(shù)詳解
- 云計算、網(wǎng)絡(luò)安全和網(wǎng)絡(luò)盜竊:網(wǎng)絡(luò)世界防盜初學(xué)指南
- 結(jié)構(gòu)化決策力:成為你想成為的自己