- Modern Web Development with ASP.NET Core 3
- Ricardo Peres
- 325字
- 2021-06-18 18:36:03
Understanding the view life cycle
When an action signals that a view should be rendered, the following occurs (in a simplified way):
- The action returns a ViewResult object because ViewResult implements IActionResult, and its ExecuteResultAsync method is called asynchronously.
- The default implementation attempts to find ViewResultExecutor from the dependency injection (DI) framework.
- The FindView method is called on ViewResultExecutor, which uses an injected ICompositeViewEngine, also obtained from the DI framework, to obtain IView from the list of registered view engines.
- The view engine chosen will be an implementation of IRazorViewEngine (which, in turn, extends IViewEngine).
- The IView implementation uses the registered IFileProviders to load the view file.
- ViewResultExecutor is then asked to invoke the view, through its ExecuteAsync method, which ends up invoking the ExecuteAsync methods of the base ViewExecutor.
- ViewExecutor builds and initializes some infrastructure objects such as ViewContext and ends up invoking IView RenderAsync method.
- Another service (ICompilationService) is used to compile the C# code.
- The registered IRazorPageFactoryProvider creates a factory method for creating a .NET class that inherits from IRazorPage.
- IRazorPageActivator is passed an instance of the new IRazorPage.
- The ExecuteAsync method of IRazorPage is called.
Here, I didn't mention the filters, but they are here as well, except action filters, as I said.
Why is this important? Well, you may need to implement your own version of—say—IRazorPageActivator so that you can perform some custom initialization or DI in the Razor view, as illustrated in the following code block:
public class CustomRazorPageActivator : IRazorPageActivator
{
private readonly IRazorPageActivator _activator;
public CustomRazorPageActivator(
IModelMetadataProvider metadataProvider,
IUrlHelperFactory urlHelperFactory,
IJsonHelper jsonHelper,
DiagnosticSource diagnosticSource,
HtmlEncoder htmlEncoder,
IModelExpressionProvider modelExpressionProvider)
{
this._activator = new RazorPageActivator(
metadataProvider,
urlHelperFactory,
jsonHelper,
diagnosticSource, htmlEncoder,
modelExpressionProvider);
}
public void Activate(IRazorPage page, ViewContext context)
{
if (page is ICustomInitializable)
{
(page as ICustomInitializable).Init(context);
}
this._activator.Activate(page, context);
}
}
All you need to do is register this implementation in ConfigureServices, for the IRazorPageActivator service, like this:
services.AddSingleton<IRazorPageActivator, CustomRazorPageActivator>();
Now, how are views located?
推薦閱讀
- 從零開始:數字圖像處理的編程基礎與應用
- Android和PHP開發最佳實踐(第2版)
- 樂學Web編程:網站制作不神秘
- 鋒利的SQL(第2版)
- 零基礎學單片機C語言程序設計
- 批調度與網絡問題的組合算法
- Service Mesh實戰:基于Linkerd和Kubernetes的微服務實踐
- Building Microservices with .NET Core
- Learning Apache Cassandra
- Oracle GoldenGate 12c Implementer's Guide
- R用戶Python學習指南:數據科學方法
- 一步一步跟我學Scratch3.0案例
- 零基礎學C++(升級版)
- Developer,Advocate!
- INSTANT EaselJS Starter