core从ActionFilterAttribute获取Request.Body的正确方式

更新时间:2023-08-03 12:57:29 阅读: 评论:0

自卑心理的主要表现core从ActionFilterAttribute获取Request.Body的正确⽅式由于 ModelBinding在动作过滤器之前运⾏,直接使⽤ context.ActionArguments["parameter"]  获取模型对象
虎啸深山
This article shows how to u an ActionFilter to validate the model from a HTTP POST request in an ASP Core MVC application. 2019-07-31: Updated to ASP Core 3.0 Preview 7, Updated to Angular 8.1.3
2019-02-16: Updated to Angular 7.2.4, ASP Core 2.2 nuget packages
2018-11-22: Updated to Angular 7.1.0, nuget packages
2018-09-28: Updated to ASP Core 2.1.4 and Angular 6.1.9
2018-06-16: Updated to ASP Core 2.1 and Angular 6.0.5
2018-02-11: Updated to ASP Core All 2.0.5 and Angular 5.2.4
2017-08-19: Updated to ASP Core 2.0 and Angular 4.3.5
2017.02.03: Updated to Angular 2.4.5 and webpack 2.2.1, VS2017 RC3, msbuild3
2016.12.23: Updated to Visual Studio 2017 and ASP Core 1.1
Other articles in this ries:
1.
2.
3. ASP Core Action Arguments Validation using an ActionFilter
佛珠的颗数含义In an ASP Core MVC application, custom validation logic can be implemented in an ActionFilter. Becau the ActionFilter is procesd after the model binding in the action execution, the model and action parameters can be ud in an ActionFilter without having to read from the Request Body, or the URL.
The model can be accesd using the context.ActionArguments dictionary. The key for the property has to match the parameter name in the MVC Controller action method.  also explained in this , that the context.ActionDescriptor.Parameters can also be ud to access the request payload data.
If the model is invalid, the context status code is t to 400 (bad request) and the reason is added to
the context result using a ContentResult object. The request is then no longer procesd but short circuited using the terminology from the ASP Core .
锁呐独奏
1
2
3
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34using System;
using System.IO;
using System.Text;
using AngularAutoSaveCommands.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
选拔任用干部必须namespace AngularAutoSaveCommands.ActionFilters
{
public class ValidateCommandDtoFilter : ActionFilterAttribute
{
private readonly ILogger _logger;
谢杏芳public ValidateCommandDtoFilter(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger("ValidatePayloadTypeFilter");
}
public override void OnActionExecuting(ActionExecutingContext context)
{
var commandDto = context.ActionArguments["commandDto"] as CommandDto;            if(commandDto == null)
{
context.HttpContext.Respon.StatusCode = 400;
context.Result = new ContentResult()
{
Content = "The body is not a CommandDto type"
};
return;
}
_logger.LogDebug("validating CommandType");
小炒肉的家常做法
34
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58            if(!CommandTypes.AllowedTypes.Contains(commandDto.CommandType))            {
context.HttpContext.Respon.StatusCode = 400;
context.Result = new ContentResult()
{
Content = "CommandTypes not allowed"
};
return;
}
_logger.LogDebug("validating PayloadType");
if(!PayloadTypes.AllowedTypes.Contains(commandDto.PayloadType))
{
鼎边糊context.HttpContext.Respon.StatusCode = 400;
context.Result = new ContentResult()
{
Content = "PayloadType not allowed"
};
return;
}
ba.OnActionExecuting(context);
}
}
}
The ActionFilter is added to the rvices in the Startup class. This is not needed if the ActionFilter is ud directly in the MVC Controller. 1rvices.AddScoped<ValidateCommandDtoFilter>();
The filter can then be ud in the MVC Controller using the ServiceFilter attribute. If the commandDto model is invalid, a BadRequest respon is returned without processing the business in the action method.
1 2 3 4 5 6 7 8[ServiceFilter(typeof(ValidateCommandDtoFilter))] [HttpPost]
[Route("Execute")]
public IActionResult Post([FromBody]CommandDto commandDto) {
_commandHandler.Execute(commandDto);
return Ok(commandDto);
}
Links

本文发布于:2023-08-03 12:57:29,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1127669.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:获取   小炒   过滤器   虎啸   颗数   锁呐   选拔   模型
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图