vault backup: 2024-11-16 23:04:28

This commit is contained in:
2024-11-16 23:04:29 +03:30
parent db574288c5
commit 4f0e91702d
-114
View File
@@ -123,117 +123,3 @@ public IActionResult EditProduct(int id, string name) {
---
## **3. Return Types in Controllers**
Return types in ASP.NET Core MVC actions define how data is sent back to the client. Different return types offer flexibility for returning views, JSON, status codes, and redirects.
### **Common Return Types**
1. **ViewResult** (`View()`)
- Renders a Razor View.
- Typically used in MVC applications where HTML is returned.
`public IActionResult Index() { return View(); }`
1. **PartialViewResult** (`PartialView()`)
- Renders a partial view, commonly used in AJAX requests.
```c#
public IActionResult ProductListPartial() { return PartialView("_ProductListPartial"); }
```
2. **JsonResult** (`Json()`)
- Returns JSON data, used often in API responses or AJAX calls.
```c#
public JsonResult GetProductJson(int id) { return Json(new { id, name = "Sample Product" }); }
```
3. **ContentResult** (`Content()`)
- Returns raw content like plain text or HTML.
```c#
public ContentResult GetPlainText()
{
return Content("Plain Text Content");
}
```
4. **FileResult** (`File()`)
- Used to send files as a response, such as for downloading PDFs or images.
```c#
public FileResult DownloadFile() { byte[] fileBytes = System.IO.File.ReadAllBytes("sample.pdf"); return File(fileBytes, "application/pdf", "sample.pdf"); }
```
5. **RedirectToActionResult** (`RedirectToAction()`)
- Redirects to a different action.
```c#
public IActionResult RedirectToHome()
{
return RedirectToAction("Index", "Home");
}
```
6. **StatusCodeResult** and **ObjectResult**
- `StatusCodeResult`: Sends HTTP status codes like `404`, `500`.
- `ObjectResult`: Often used in APIs to return a model or object with a status code.
```c#
public IActionResult NotFoundResult()
{
return StatusCode(404);
}
```
7. **ChallengeResult** and **SignOutResult**
- `ChallengeResult`: Used to trigger authentication challenges.
- `SignOutResult`: Logs out the user.
```c#
public IActionResult SignOutUser() { return SignOut(); }
```
---
## **Putting It All Together: A Complete Flow**
Heres how you can use routing, controllers, and return types together to create an application with clear structure and varied responses.
1. **Configure Routing** in `Program.cs` with both default and custom routes.
2. **Define Controllers** for different areas of the application, organizing actions based on functionality and HTTP methods.
3. **Handle Different Requests and Return Types**:
- For UI, use `ViewResult` or `PartialViewResult`.
- For API endpoints, use `JsonResult` and `ObjectResult`.
- Redirect or return status codes based on user actions.
By mastering routing, controllers, and return types in this way, youll be able to create well-structured ASP.NET Core MVC applications that respond appropriately to a wide variety of requests. This roadmap provides a full foundation, allowing you to expand into more complex scenarios as you gain experience.
| Return Type | Description |
| ---------------- | ------------------------------------------------- |
| `Ok()` | Returns 200 OK with optional content. |
| `NotFound()` | Returns 404 Not Found. |
| `BadRequest()` | Returns 400 Bad Request. |
| `Unauthorized()` | Returns 401 Unauthorized. |
| `Forbid()` | Returns 403 Forbidden. |
| `Created()` | Returns 201 Created with a URI for the new item. |
| `NoContent()` | Returns 204 No Content, typically for DELETE/PUT. |
| `Redirect()` | Redirects to another URL. |
| `Content()` | Returns plain text or other content. |
| `Json()` | Returns JSON data. |
| `File()` | Returns a file. |