经验分享 · 2025年7月12日

.net core mini api在接收文件上传时提示:System.InvalidOperationException: Endpoint HTTP: POST /upload_xxx contains anti-forgery metadata…

.DisableAntiforgery() helps with error in topic, but catched another with binding IFormFile.. Then I took file from request manually and it’s worked:

The best solution is:

Get the XSRF token before upload file; post token when upload.

So I’ve got 2 endpoints now:

Upload the file

Step 1: Get the token.

Step 2.1: Before making the POST call to Upload endpoint, add the XSRF token you received from Step 1.

Step 2.2: Add the file you want to upload. pickle.png in my case.

Step 2.3: Hit send. At this point, you’ll be able to make a successful call.