1、先看代码
/// <summary>
/// 自定义
/// 读取上传内容的过滤器
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class ReadContentFileter : Attribute, IActionFilter
{
/// <summary>
/// 执行后
/// </summary>
/// <param name="context"></param>
public void OnActionExecuted(ActionExecutedContext context)
{
Console.WriteLine(context.ToString());
}
/// <summary>
/// 执行前
/// </summary>
/// <param name="context"></param>
public void OnActionExecuting(ActionExecutingContext context)
{
var syncIOFeature = context.HttpContext.Features.Get<IHttpBodyControlFeature>();
if (syncIOFeature != null)
{
syncIOFeature.AllowSynchronousIO = true;
}
StreamReader stream = new StreamReader(context.HttpContext.Request.Body);
string body = stream.ReadToEnd();
Console.WriteLine(body);
}
}
2、内容解释
????????1、IActionFilter 对方法的接口(包含方法执行前和执行后的接口)
????????2、Attribute 打上可注解的标记
????????3、AttributeUsage 标注是可添加在类上 | 可添加在方法上,不允许同时注解多个
3、使用方法
/// <summary>
/// 上传
/// </summary>
/// <returns></returns>
[HttpPost]
[ReadContentFileter]
public Task<IActionResult> FileSaveBL2(string str)
{
return FileBZ("BL");
}
4、效果
执行完上面的方法后才会执行调用的方法