问题描述
我要确定是否应该执行 OnPost()
的页面很多.
I have a good number of pages I want to determine whether I should allow the OnPost()
to be executed.
我想知道是否可以/在执行 OnPost()
来潜在地阻塞 OnPost()
调用之前执行代码块的最佳方式是?
I was wondering if you can/what the best way of executing a block of code before the OnPost()
takes place to potentially block the OnPost()
call is?
我知道您可以在每个 OnPost()
的顶部进行检查,并调用一些对象/类方法来指示,但是还有其他方法可以在 OnPost()
甚至是针对一组特定的页面执行的?
I know you could do a check at the top of every OnPost()
and call some object/class method to dictate but is there some other way that would execute before the OnPost()
is even executed for a specific set of pages?
(使用Razor Pages,而不是MVC)
(Using Razor Pages, not MVC)
推荐答案
可以在选择处理程序方法之后但在执行之前使用筛选器执行代码:
You can use a Filter to execute code after the handler method has been selected, but before it has been executed:
public override void OnPageHandlerSelected(PageHandlerSelectedContext context)
{
if(context.HandlerMethod.MethodInfo.Name == nameof(OnPost))
{
// code placed here will only execute if the OnPost() method has been selected
}
}
https://www.learnrazorpages.com/razor-pages/filters
这篇关于在设置页面的.cshtml.cs文件中执行OnPost()方法之前,是否可以执行特定的代码块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!