0
1

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 3 years have passed since last update.

.Net6ではstringもNull許容にしないとPostでバリデーションエラーになる

Last updated at Posted at 2022-08-18

.net core3.1の頃のプログラムを.net6に移行したら…

APIで何故か400エラーが…!

クライアント(react)からaxiosでサーバー側(.net6)にPOSTリクエストするだけのなんてことない普通のAPIです。

API Testerで叩いてみる

返ってきたレスポンスを見てみます。

{
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "00-xxxxxxxxxxxxxxxxxxxx-xxxxxxxxx-00",
  "errors":{
    "Text":[
      "The Text field is required."
    ],
  }
}

validation errors ばりでーしょんえらー??
なぜ??:disappointed_relieved:

The Text field is required.
必須入力にした覚えなんかないけども…

そういえば.net6になってからこれを良く見るぞ:astonished:

image.png

string型もnull許容型にしないとダメよん!と指摘(怒られてはいない!笑)されるので
visual studio君のいう通りに直しておりました。

image.png

なるほど…「Null許容しない=必須やで」ということなのか:anguished:

コンパイルエラーにはならないのが詰まるミソですねこれ。

Null許容型に変えてみる

修正前

public string Text { get; set; }

修正後

public string? Text { get; set; }

結果

200!OK!!:point_left:

なんてことない問題だったんですが、
なんでや!!と詰まったのでメモメモ。

もし同じ現象で困ってる方がいましたら、ちょっとでも参考になれば幸いです。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?