چه از web api استفاده کنیم یا MVC controller این دوتا هر جفتشون از Controller base ارث بری میکنن و IACTION Result رو برمیگردونن
خوب اینجا داره میگه که خیلی دستمون بازه توی middleware ها هم pipeline سمت response و هم request از یه middleware دارن استفاده میکنن
IActionResult #ViewResult #jsonResult
IActionResult
is an interface in ASP.NET Core MVC that represents the result of an action method in a controller. When an action method in a controller returns a result, it can return different types of responses, such as rendering a view, redirecting to another action, returning a JSON object, or sending a file. The IActionResult
interface provides a common way to represent all of these possible outcomes.
Common Implementations of IActionResult
:
Here are some common classes that implement IActionResult
:
-
ViewResult
:- Represents a rendered view.
- Example:
return View(model);
-
JsonResult
:- Represents a JSON-formatted response.
- Example:
return Json(data);
-
RedirectResult
:- Represents a redirection to another URL.
- Example:
return Redirect("https://example.com");
-
RedirectToActionResult
:- Represents a redirection to another action within the same application.
- Example:
return RedirectToAction("ActionName");
-
ContentResult
:- Represents a plain text response.
- Example:
return Content("Hello World");
-
FileResult
:- Represents a response that contains a file (e.g., for download).
- Example:
return File(fileBytes, "application/pdf");
-
StatusCodeResult
:- Represents a response with a specific HTTP status code.
- Example:
return StatusCode(404);
-
EmptyResult
:- Represents a response that does not have any content.
- Example:
return new EmptyResult();
-
Why Use
IActionResult
?
Using IActionResult
as the return type in action methods gives you the flexibility to return different kinds of responses from the same method. It also adheres to the principle of programming to an interface, making your code more flexible and testable.
بهمون این قابلیت رو میده که چیزهای مختلفی رو توی return برگردونیم