如何将 IPageFilter 仅应用于 Razor 页面中的 OnPost 处理程序

问题描述 投票:0回答:2

我想将

IPageFilter
应用于 razor 页面中的
OnPost
处理程序。 像这样:

[MyFilter] 
public IActionResult OnPost() {  // any code }

但是当我这样做时,Visual studio 强迫我将过滤器应用到上面的类。

c# asp.net asp.net-core razor-pages
2个回答
0
投票

ASP.NET Core 中 Razor Pages 的过滤方法我们可以看到

Razor 页面过滤器 IPageFilter 和 IAsyncPageFilter 允许 Razor 页面 在运行 Razor 页面处理程序之前和之后运行代码。剃刀页面 过滤器类似于 ASP.NET Core MVC 操作过滤器,不同之处在于它们 不能应用于单个页面处理程序方法。

如果您确实想向处理程序添加过滤器,您可以在 GitHub 上提交新功能请求。


0
投票

由于您只能将

IPageFilter
属性应用于
PageModel
而不是其单独的页面处理程序方法,因此合适的解决方案是验证在过滤器实现的
OnPageHandlerSelected
方法中调用处理程序方法。

示例代码实现如下:

using Microsoft.AspNetCore.Mvc.Filters;

public class CustomPageFilter : IPageFilter
{
    public void OnPageHandlerSelected(PageHandlerSelectedContext context)
    {
        if (context.HandlerMethod.IsDefaultPost())
        {
            // Code to execute when the page handler method is selected
            Log("Page handler method selected");
        }
    }

    public void OnPageHandlerExecuting(PageHandlerExecutingContext context)
    {
        if (context.HandlerMethod.IsDefaultPost())
        {
            // Code to execute before the page handler method
            Log("Executing before the page handler method");
        }
    }

    public void OnPageHandlerExecuted(PageHandlerExecutedContext context)
    {
        if (context.HandlerMethod.IsDefaultPost())
        {
            // Code to execute after the page handler method
            Log("Executed after the page handler method");
        }
    }

    private void Log(string message)
    {
        // Your logging logic here
    }
}

IsDefaultPost
是一个扩展方法,用于验证调用处理程序是否是主要/默认 POST 处理程序方法。在此方法中,我们验证处理程序方法名称为
null
并且 HttpMethod 为
POST

扩展代码如下:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure;

public static class HandlerMethodDescriptorExtensions
{
    public static bool IsDefaultGet(this HandlerMethodDescriptor handlerMethod)
    {
        return string.IsNullOrEmpty(handlerMethod.Name) && handlerMethod.HttpMethod == HttpMethods.Get;
    }
    public static bool IsDefaultPost(this HandlerMethodDescriptor handlerMethod)
    {
        return string.IsNullOrEmpty(handlerMethod.Name) && handlerMethod.HttpMethod == HttpMethods.Post;
    }
    public static bool IsMethodName(this HandlerMethodDescriptor handlerMethod, string handlerMethodName)
    {
        return !string.IsNullOrEmpty(handlerMethod.Name) && handlerMethod.Name == handlerMethodName;
    }
    public static bool IsMethodName(this HandlerMethodDescriptor handlerMethod, string handlerMethodName, string httpMethod)
    {
        return !string.IsNullOrEmpty(handlerMethod.Name) && handlerMethod.Name == handlerMethodName && handlerMethod.HttpMethod == httpMethod;
    }
}

此外,您可以使用

IsDefaultGet()
来验证调用处理程序方法是否是主要
GET
方法。您还可以使用
IsMethodName(,)
来确定调用处理程序方法是否与您指定的名称匹配。

例如:

bool isMyMethod = context.HandlerMethod.IsMethodName("MyMethod");

或者,如果您希望包含 HttpMethod 参数:

bool isMyMethod = context.HandlerMethod.IsMethodName("MyMethod", HttpMethods.Post);
© www.soinside.com 2019 - 2024. All rights reserved.