Help us understand the problem. What is going on with this article?

「ASP.NET Web API でモデルの検証」を試してみる

背景

ASP.NET Web APIで「モデルの検証処理」の実装が必要だったので、試した際の備忘録

今回試した内容

  • 必須の値の検証(Required)
  • 最大文字数の検証(MaxLength)
  • 数値の最小範囲~最大範囲の検証(Range)

開発環境

  • Windows 10
  • Visual Studio 2017
  • .NET Framework 4.7.2

参考にした資料

実装

ASP.NET Web API でモデルの検証に沿って、ModelとControllerを作成。

User.cs
//Using System.ModelComponentModel.DataAnotaions が必要。
public class User
 {
    [MaxLength(4)]
    [Required]
    public string UserID { get; set; }
    public string UserName { get; set; }
    [Range(0, 999)]
    public int Age { get; set; }
    public DateTime BirthDay { get; set; }
    public bool IsActive { get; set; }
}
UsersController.cs
public class UsersController : ApiController
{
    public HttpResponseMessage Post(User user)
    {
        if (ModelState.IsValid)
        {
            return new HttpResponseMessage(HttpStatusCode.OK);
        }
        else
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
        }
    }
}

実行結果

1. UserIDの文字数制限を超過した場合

RequestBody.json
{
    "userid":"user1"
}
ResponseBody.json
{
    "Message":"The request is invalid.",
    "ModelState":{
        "user.UserID":[
            "フィールド UserID は、最大の長さが '4' の文字列型または配列型でなければなりません。"
        ]
    }
}

2. UserIDを指定しなかった場合

RequestBody.json
{
    "age":27
}
ResponseBody.json
{
    "Message":"The request is invalid.",
    "ModelState":{
        "user.UserID":[
            "UserID フィールドが必要です。"
        ]
    }
}

3. Ageの範囲外の値を指定した場合

RequestBody.json
{
    "userid":"user",
    "age":1000
}
ResponseBody.json
{
    "Message":"The request is invalid.",
    "ModelState":{
        "user.Age":[
            "フィールド Age は 0 から 999 までの範囲で指定してください。"
        ]
    }
}

4. 「UserIDの文字数超過」 かつ 「Ageの範囲外の値」を指定した場合

RequestBody.json
{
    "userid":"user1",
    "age":1000
}
ResponseBody.json
{
    "Message":"The request is invalid.",
    "ModelState":{
        "user.UserID":[
            "フィールド UserID は、最大の長さが '4' の文字列型または配列型でなければなりません。"
        ],
        "user.Age":[
            "フィールド Age は 0 から 999 までの範囲で指定してください。"
        ]
    }
}

今後試したい内容

  • プロパティの型の検証
  • エラーメッセージのカスタマイズ
  • RFC7807に準拠したエラーレスポンス形式でエラーを返す
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away