.NET Core 3.1接收微信回调

1.编写WechatNotifyAttribute

由于控制器拿不到request,需要在过滤器注入参数

public class WechatNotifyAttribute: ActionFilterAttribute
{
     
    public override void OnActionExecuting(ActionExecutingContext filterContext) {
     
        var syncIOFeature = filterContext.HttpContext.Features.Get<IHttpBodyControlFeature>();
        if (syncIOFeature != null)
        {
     
            syncIOFeature.AllowSynchronousIO = true;
        }
        var request = filterContext.HttpContext.Request;
        request.EnableBuffering();
        var reader = new StreamReader(request.Body);

        string content = reader.ReadToEnd();
        request.Body.Position = 0;
        filterContext.ActionArguments.Add("content", content);
        base.OnActionExecuting(filterContext);
    }
}

2.使用方式

[WechatNotify]
public async Task<string> GzhPayNotify(string content)
{
     
    Log.Debug("微信充值回调开始:\n"+content);
    ......
}

你可能感兴趣的:(C##dotnet,core,.net,c#)