且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

涉足ASP.Net Core的剃须刀页面执行

更新时间:2023-02-14 18:15:37

好,看来您必须使用ViewResultExecutor.在对代码进行了进一步的摸索之后,我发现执行程序被用来调用嵌套的ExecuteAsync调用链中的第一个ExecuteAsync(). ;)

Ok, looks like you have to use ViewResultExecutor. After more spelunking through the code, I found that an executor was used to call the first ExecuteAsync() in a chain of nested ExecuteAsync calls. ;)

public class MyViewResultExecutor : ViewResultExecutor
{
    ....
    public override Task ExecuteAsync(ActionContext actionContext, IView view, ViewResult viewResult) ....
    ....
}
....
services.TryAddSingleton<ViewResultExecutor, MyViewResultExecutor>();

ViewResultExecutor服务对象是在ViewResult.ExecuteResultAsync(ActionContext context)中获得的.

The ViewResultExecutor service object is obtained in ViewResult.ExecuteResultAsync(ActionContext context).

很棒的是,您还可以通过view参数((view as RazorView)?.RazorPage)访问自定义页面类型. ;)(当然,您必须将其强制转换为自定义类型)

The great thing is you can also access your custom page type through the view parameter ((view as RazorView)?.RazorPage). ;) (though, of course, you'll have to cast it to your custom type)

(我最初在此处开始讨论,如果有人对此感兴趣阅读ASP.Net Core MVC Source方面的更多详细信息)

(I started a discussion here originally if anyone is interested to read a few more details on the ASP.Net Core MVC Source side of things)

更新:自最初发布以来,此过程已更改.这是注册您自己的执行者的新方法:

Update: This process has changed since this was originally posted. This is the new way to register your own executor:

services.TryAddSingleton<IActionResultExecutor<ViewResult>, MyViewResultExecutor>();
// ... or ...
services.TryAddSingleton<IActionResultExecutor<PartialViewResult>, MyPartialViewResultExecutor>();

请注意TryAdd部分.这意味着如果它已经存在,它将不会添加.这是MVC代码尝试执行的操作,因此您必须在MVC之前先注册自己的代码.同样,如果从ViewResultExecutor(而不是它实现的接口)派生,则{ViewResultExecutor}.ExecuteAsync(...)签名已更改,不能再被覆盖.您现在只能覆盖基本的{ViewExecutor}.ExecuteAsync(...)方法.

Please notice the TryAdd part. That means it will not add it if it already exists. This is the same thing the MVC code tries to do, so you must register yours FIRST before MVC does. Also, if deriving from ViewResultExecutor (instead of the interface it implements) the {ViewResultExecutor}.ExecuteAsync(...) signature has changed and can no longer be overridden. You can only override the base {ViewExecutor}.ExecuteAsync(...) method now.