The IActionResult interface is a core concept in ASP.NET Core MVC. It serves as the return type for action methods in your controllers, providing flexibility and enabling you to return different types of responses depending on the context of the request.
Essentially, it’s a contract that defines a single method: Task ExecuteResultAsync(ActionContext context);
This method is responsible for executing the specific logic associated with the action result, generating the appropriate HTTP response that’s sent back to the client.
Action Result Types Here’s a breakdown of some of the most important action result types derived from IActionResult: ContentResult: Returns a string as raw content (text, HTML, XML, etc.). Example: return Content(“Hello from Index”, “text/plain”); EmptyResult: Represents an empty response (204 No Content). Example: return new EmptyResult(); FileResult: Used to send files to the client (PDF, images, etc.). This is a base class for several more specific file result types. VirtualFileResult: Serves a file from the web root or a virtual path. PhysicalFileResult: Serves a file from a physical path on the server. FileContentResult: Serves a file from an in-memory byte array. JsonResult: Serializes an object into JSON format and sends it as the response. Example: return Json(new { message = “Success” }); RedirectResult: Redirects the user to a different URL. Example: return Redirect(“/home”); RedirectToActionResult: Redirects to a specific action method in a controller. Example: return RedirectToAction(“Index”, “Home”); ViewResult: Renders a view, typically an HTML page, with optional model data. Example: return View(“Index”, model); PartialViewResult: Renders a partial view (a reusable portion of a view). Example: return PartialView(“_ProductCard”, product); StatusCodeResult: Returns a specific HTTP status code with an optional message. Example: return StatusCode(404, “Resource not found”); BadRequestResult: Shorthand for returning a 400 Bad Request response. NotFoundResult: Shorthand for returning a 404 Not Found response. OkResult: Shorthand for returning a 200 OK response.
Code // HomeController.cs [Route(“book”)] public IActionResult Index() { // Book id should be applied if (!Request.Query.ContainsKey(“bookid”)) { Response.StatusCode = 400; // Setting status code manually return Content(“Book id is not supplied”); }
// … other validation checks …
// If all checks pass return File(“/sample.pdf”, “application/pdf”); }
In this action method: Validation: The code performs several validation checks on the bookid query parameter: It checks if the parameter exists. It checks if the parameter value is not null or empty. It checks if the parameter value is within a valid range (1-1000). It checks if the isloggedin query parameter is true. Error Responses: If any validation fails, a ContentResult is returned with an appropriate error message and a 400 Bad Request or 401 Unauthorized status code. Successful Response: If all validation passes, a FileResult is returned, serving the sample.pdf file from the web root.