本文介绍了涉足ASP.Net Core的剃须刀页面执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用我的CUSTOM视图页面(继承自RazorPage)来挂接渲染剃刀页面的ExecuteAsync()调用.在RazorPage类中,有一个抽象方法:

I'm trying to hook into the ExecuteAsync() call that renders a razor page using my CUSTOM view page (that inherits from RazorPage). In the RazorPage class there is this abstract method:

public abstract Task ExecuteAsync();

在解析.cshtml文件(视图)时,该方法在razor生成的输出中被调用.

That method gets called within the output generated by razor when parsing a .cshtml file (view).

很明显,我不能仅仅重写它,因为我永远都不会从生成的视图中调用我的视图,它也将重写此方法(尽管那样很好,并且至少可以解决部分问题).

Obviously, I cannot just override it because mine would never get called from the view that gets generated, which also overrides this method (though that would be nice, and solve at least part of the problem).

.Net核心中是否有任何特殊的剃须刀技巧,可以在渲染过程之后的 AND 之前进行拦截? (使用我的自定义类)

Is there any special razor tricks in .Net core where I can intercept before AND after the rendering process? (using my custom class that is)

推荐答案

好,看来您必须使用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.

这篇关于涉足ASP.Net Core的剃须刀页面执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 17:22