IActionResult_ReturnTypes
In ASP.NET Core, IActionResult represents the result of an action method in an MVC controller. The IActionResult interface defines various types of results that can be returned from action methods. Here are some common return types of IActionResult:
- ViewResult: Represents an HTML view that is rendered to the client.
- PartialViewResult: Represents a partial HTML view that is rendered to the client.
- ContentResult: Represents a text or content result that is directly written to the response body.
- JsonResult: Represents a JSON result that is serialized and sent to the client.
- RedirectResult: Represents a redirection to a specified URL.
- RedirectToActionResult: Represents a redirection to an action method with specified controller, action, and route values.
- RedirectToRouteResult: Represents a redirection to a specific route.
- FileResult: Represents a file download result.
- StatusCodeResult: Represents a specific HTTP status code result.
- ObjectResult: Represents an arbitrary object that is serialized and sent to the client.
- BadRequestResult: Represents a 400 Bad Request status code.
- NotFoundResult: Represents a 404 Not Found status code.
- UnauthorizedResult: Represents a 401 Unauthorized status code.
- ForbidResult: Represents a 403 Forbidden status code.
- ChallengeResult: Represents a 401 Unauthorized status code with a challenge.
Each of these return types corresponds to a specific HTTP response that can be returned from an action method in an ASP.NET Core MVC application.
Examples:
using Microsoft.AspNetCore.Mvc; public class DemoController : Controller { // ViewResult: Renders a view named "Index" public IActionResult Index() { return View(); } // PartialViewResult: Renders a partial view named "_PartialView" public IActionResult Partial() { return PartialView("_PartialView"); } // ContentResult: Returns plain text content public IActionResult Content() { return Content("This is plain text content."); } // JsonResult: Returns JSON data public IActionResult JsonData() { var data = new { Name = "John", Age = 30 }; return Json(data); } // RedirectResult: Redirects to a specific URL public IActionResult RedirectToUrl() { return Redirect("https://example.com"); } // RedirectToActionResult: Redirects to another action method public IActionResult RedirectToAction() { return RedirectToAction("Index", "Home"); } // RedirectToRouteResult: Redirects to a specific route public IActionResult RedirectToRoute() { return RedirectToRoute(new { controller = "Home", action = "Index" }); } // FileResult: Returns a file for download public IActionResult DownloadFile() { byte[] fileBytes = System.IO.File.ReadAllBytes(@"C:\path\to\file.txt"); return File(fileBytes, "text/plain", "file.txt"); } // StatusCodeResult: Returns a specific HTTP status code public IActionResult StatusCodeResult() { return StatusCode(404); // 404 Not Found } // ObjectResult: Returns an arbitrary object serialized as JSON public IActionResult ObjectData() { var data = new { Name = "Jane", Age = 25 }; return new ObjectResult(data); } // BadRequestResult: Returns a 400 Bad Request status code public IActionResult BadRequestResult() { return BadRequest(); } // NotFoundResult: Returns a 404 Not Found status code public IActionResult NotFoundResult() { return NotFound(); } // UnauthorizedResult: Returns a 401 Unauthorized status code public IActionResult UnauthorizedResult() { return Unauthorized(); } // ForbidResult: Returns a 403 Forbidden status code public IActionResult ForbidResult() { return Forbid(); } // ChallengeResult: Returns a 401 Unauthorized status code with a challenge public IActionResult ChallengeResult() { return Challenge(); } }
Certainly! Here are examples of each return type of IActionResult with corresponding action methods in an ASP.NET Core MVC controller:
csharpusing Microsoft.AspNetCore.Mvc;
public class DemoController : Controller
{
// ViewResult: Renders a view named "Index"
public IActionResult Index()
{
return View();
}
// PartialViewResult: Renders a partial view named "_PartialView"
public IActionResult Partial()
{
return PartialView("_PartialView");
}
// ContentResult: Returns plain text content
public IActionResult Content()
{
return Content("This is plain text content.");
}
// JsonResult: Returns JSON data
public IActionResult JsonData()
{
var data = new { Name = "John", Age = 30 };
return Json(data);
}
// RedirectResult: Redirects to a specific URL
public IActionResult RedirectToUrl()
{
return Redirect("https://example.com");
}
// RedirectToActionResult: Redirects to another action method
public IActionResult RedirectToAction()
{
return RedirectToAction("Index", "Home");
}
// RedirectToRouteResult: Redirects to a specific route
public IActionResult RedirectToRoute()
{
return RedirectToRoute(new { controller = "Home", action = "Index" });
}
// FileResult: Returns a file for download
public IActionResult DownloadFile()
{
byte[] fileBytes = System.IO.File.ReadAllBytes(@"C:\path\to\file.txt");
return File(fileBytes, "text/plain", "file.txt");
}
// StatusCodeResult: Returns a specific HTTP status code
public IActionResult StatusCodeResult()
{
return StatusCode(404); // 404 Not Found
}
// ObjectResult: Returns an arbitrary object serialized as JSON
public IActionResult ObjectData()
{
var data = new { Name = "Jane", Age = 25 };
return new ObjectResult(data);
}
// BadRequestResult: Returns a 400 Bad Request status code
public IActionResult BadRequestResult()
{
return BadRequest();
}
// NotFoundResult: Returns a 404 Not Found status code
public IActionResult NotFoundResult()
{
return NotFound();
}
// UnauthorizedResult: Returns a 401 Unauthorized status code
public IActionResult UnauthorizedResult()
{
return Unauthorized();
}
// ForbidResult: Returns a 403 Forbidden status code
public IActionResult ForbidResult()
{
return Forbid();
}
// ChallengeResult: Returns a 401 Unauthorized status code with a challenge
public IActionResult ChallengeResult()
{
return Challenge();
}
}
Each action method in the above controller demonstrates a different return type of IActionResult along with its usage.
Comments
Post a Comment