vault backup: 2024-11-21 12:12:45

This commit is contained in:
2024-11-21 12:12:45 +03:30
parent aca5f79103
commit 11c541fe65
206 changed files with 111329 additions and 12 deletions
@@ -0,0 +1,60 @@
## View Components
[View components](https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-9.0) are similar to partial views in that they allow you to reduce repetitive code, but they're appropriate for view content that requires code to run on the server in order to render the webpage. View components are useful when the rendered content requires database interaction, such as for a website shopping cart. View components aren't limited to model binding in order to produce webpage output.
## ViewImports and ViewStart
In Razor Pages, the application of `ViewStart.cshtml` and `ViewImports.cshtml` is governed by their **location in the folder hierarchy**. These files are automatically discovered and applied by ASP.NET Core's Razor engine based on the folder structure.
---
### **How `ViewStart.cshtml` Works in Razor Pages**
1. **Global Scope:**
- A `ViewStart.cshtml` file placed in the root of the `Pages` folder applies to all Razor Pages in the project.
- Example structure:
`Pages/ ├── _ViewStart.cshtml ├── Index.cshtml ├── About.cshtml`
If `_ViewStart.cshtml` contains:
`@{ Layout = "_Layout"; }`
Then `Index.cshtml` and `About.cshtml` will use the `_Layout` layout.
2. **Local Override:**
- If a `ViewStart.cshtml` exists in a subfolder, it **overrides** the `ViewStart.cshtml` in the parent folder for that subfolder and its descendants.
- Example structure:
`Pages/ ├── _ViewStart.cshtml ├── Admin/ │ ├── _ViewStart.cshtml │ ├── Dashboard.cshtml └── Index.cshtml`
- The `_ViewStart.cshtml` in `Pages/Admin/` will apply to `Dashboard.cshtml`.
- The `_ViewStart.cshtml` in the root will apply to `Index.cshtml`.
---
### **How `ViewImports.cshtml` Works in Razor Pages**
1. **Global Scope:**
- A `ViewImports.cshtml` file placed in the root of the `Pages` folder applies to all Razor Pages in the project.
- Example structure:
`Pages/ ├── _ViewImports.cshtml ├── Index.cshtml ├── Contact.cshtml`
If `_ViewImports.cshtml` contains:
`@using MyApp.Models @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers`
Then both `Index.cshtml` and `Contact.cshtml` will have access to the `MyApp.Models` namespace and the Tag Helpers.
2. **Local Override or Supplement:**
- A `ViewImports.cshtml` file in a subfolder **adds to** or **overrides** the settings from the parent folder.
- Example structure:
`Pages/ ├── _ViewImports.cshtml ├── Admin/ │ ├── _ViewImports.cshtml │ ├── Dashboard.cshtml └── Index.cshtml`
- The `@using` directives and Tag Helpers in `Pages/Admin/_ViewImports.cshtml` will only apply to `Dashboard.cshtml`.
- The root `Pages/_ViewImports.cshtml` still applies to `Index.cshtml`.
---
### **How Razor Pages Discover These Files**
- **Runtime Discovery:** The Razor engine searches for these files in the **current folder** and all parent folders (up to the root).
- **Hierarchical Application:**
- `ViewStart.cshtml` and `ViewImports.cshtml` in a closer (nested) folder override or extend those in parent folders.
- This hierarchy allows flexible configuration for specific parts of the application.
---
### **Best Practices for Razor Pages**
1. Place shared settings (e.g., default layout or common namespaces) in the root-level `ViewStart.cshtml` and `ViewImports.cshtml`.
2. Use folder-specific overrides sparingly to avoid confusion.
3. Keep these files clean and limited to truly shared settings or imports.
Let me know if you'd like an example project structure for clarification!