.NET Core 3.1接收微信回调

  • Post author:
  • Post category:其他

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);
    ......
}

版权声明:本文为flytsuki原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。