Razor Compilation là gì?
Razor files với extension.cshtml được compile tại build time và publish time sử dụng Razor SDK. Runtime compilation có thể được bật tùy chọn bằng cách cấu hình project.
Lưu ý: Runtime compilation đã bị obsolete từ .NET 10. Không hỗ trợ cho Razor components của Blazor apps. Không hỗ trợglobal using directivesvàimplicit using directives.
Razor Compilation
Build-time và Publish-time Compilation
Mặc định, Razor SDK enable compilation tại thời điểm build và publish. Khi enabled, runtime compilation bổ sung cho build-time compilation, cho phép Razor files được cập nhật nếu được chỉnh sửa trong khi app đang chạy.So sánh các loại Compilation
| Loại compilation | Mô tả | Khi nào dùng |
|---|---|---|
| Build-time | Compile khi build project | Production |
| Publish-time | Compile khi publish project | Deployment |
| Runtime | Compile khi app đang chạy | Development (đã obsolete) |
Hot Reload thay thế Runtime Compilation
Lưu ý: Runtime compilation vô hiệu hóa .NET Hot Reload. Thay vào đó, Microsoft khuyến nghị sử dụng Hot Reload cho development scenarios.
Khuyến nghị
| Scenario | Khuyến nghị |
|---|---|
| Production | Default build-time compilation |
| Development | Hot Reload (.NET) |
| Runtime Compilation | Deprecated, không khuyến khích |
Enable Runtime Compilation cho tất cả Environments
Bước 1: Cài đặt NuGet Package
Bước 2: Cấu hình trong Program.cs
Enable Runtime Compilation có điều kiện
Runtime compilation có thể được enable có điều kiện, đảm bảo rằng published output:- Sử dụng compiled views
- Không enable file watchers trong production
Cách 1: Kiểm tra Environment trong Program.cs
Cách 2: Hosting Startup Assembly
Runtime compilation cũng có thể được enable với hosting startup assembly. Cách này không cần thay đổi code trongProgram.cs.
Yêu cầu
- Install
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilationNuGet package - Đặt
ASPNETCORE_ENVIRONMENTlà"Development" - Set
ASPNETCORE_HOSTINGSTARTUPASSEMBLIESlà"Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
launchSettings.json
Cách hoạt động
At runtime, ASP.NET Core tìm kiếm một assembly-level HostingStartup attribute trongMicrosoft.AspNetCore.Mvc.Razor.RuntimeCompilation. HostingStartup attribute chỉ định code startup để execute và enable runtime compilation.
Enable Runtime Compilation cho Razor Class Library
Kịch bản
Một Razor Pages project tham chiếu đến Razor Class Library (RCL) tênMyClassLib. RCL chứa file _Layout.cshtml được sử dụng bởi MVC và Razor Pages projects.
Bước 1: Enable Runtime Compilation có điều kiện
Làm theo hướng dẫn Enable runtime compilation conditionally ở trên.Bước 2: Cấu hình MvcRazorRuntimeCompilationOptions
Giải thích
- Code trên xây dựng absolute path đến
MyClassLibRCL PhysicalFileProviderAPI được sử dụng để locate directories và files tại absolute path đóPhysicalFileProviderinstance được thêm vào file providers collection, cho phép truy cập RCL’s.cshtmlfiles
Razor SDK Properties
Các Property quan trọng
| Property | Mô tả | Giá trị mặc định |
|---|---|---|
RazorCompileOnBuild | Compile Razor tại build time | true |
RazorCompileOnPublish | Compile Razor tại publish time | true |