1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ASP.net core 3.1で Web API の Validateを試してみる

Last updated at Posted at 2020-06-12

core 3.1で
ActionFilterAttributeを使ってみたのですが
ずいぶん変わっているようですね?

ValidateIdAttribute.cs

using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.Logging;

namespace FiltersSample.Filters
{
    public class ValidateIdAttribute : ActionFilterAttribute
    {
        // パラメータのnullチェックをするかどうか
        public string ParameterName { get; set; }
        private readonly ILogger<ValidateIdAttribute> _logger;

        public ValidateIdAttribute()
        {
            _logger = new LoggerFactory().CreateLogger<ValidateIdAttribute>();
            ParameterName = "id";
        }

        public override void OnActionExecuting(ActionExecutingContext actionContext)
        {
            if (!actionContext.ActionArguments.All(x => x.Key != ParameterName || checkValue(x.Value as string ?? string.Empty)))
            {
                // 数字じゃない
                string message = $"バリデート:{ParameterName}に数字以外が含まれます。";
                actionContext.ModelState[ParameterName].Errors.Add(message);
                actionContext.ModelState[ParameterName].ValidationState = ModelValidationState.Invalid;
                _logger.LogError(message);
                actionContext.Result = new BadRequestObjectResult(actionContext.ModelState);
                //actionContext.Result = new BadRequestResult();// こっちだと既定のbodyが返る
            }
            else if (!actionContext.ModelState.IsValid)
            {
                // Data Annotationsでひっかかった
                string message = string.Join("; ", actionContext.ModelState.Select(pair=>$"{pair.Key} = {pair.Value.RawValue.ToString()} "));
                _logger.LogError($"バリデート:{message}");
                actionContext.Result = new BadRequestObjectResult(actionContext.ModelState);
                //actionContext.Result = new BadRequestResult();// こっちだと既定のbodyが返る
            }

            // その他チェックすべきものがあれば実装

            base.OnActionExecuting(actionContext);
        }


        private bool checkValue(string id)
        {
            return id.Length == 12 && id.All(char.IsDigit);
        }
    }
}

と書いて
呼び出すのは

IdController.cs

using FiltersSample.Filters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace FiltersSample.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class IdController : ControllerBase
    {
        // GET: api/<IdController>
        [HttpGet]
        [ValidateId(ParameterName = "id")]
        public IEnumerable<string> Get(string id)
        {
            return new string[] { "id", id };
        }
    }
}

な感じでいいいみたいです

https://localhost:xxxx/api/Id?id=12345678
な感じでリクエストしてみてください

サンプル
https://dev.azure.com/lowguy/_git/sample?path=%2F3.1sample

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?