Web API 2中POST请求返回404错误,但GET请求正常,求排查原因
Hey there! Let's figure out why your POST request is returning a 404 while GET works perfectly. Here are the most common issues to check, along with fixes:
1. Mismatched Route Configuration
First, double-check your controller and POST method routing attributes. If your controller uses [Route("api/[controller]")], make sure your POST method has the correct [HttpPost] attribute (with or without a specific route template).
For example:
[ApiController] [Route("api/[controller]")] public class MyApiController : ControllerBase { // Correct basic POST setup [HttpPost] // Matches POST to api/myapi public IActionResult Post([FromBody] RequestModel model) { // Your logic here return Ok(); } // Or if you need a parameter in the route [HttpPost("{appKey}")] // Matches POST to api/myapi/your-app-key public IActionResult Post(string appKey) { // Your logic here return Ok(); } }
If your POST method is missing the [HttpPost] attribute entirely, ASP.NET won't map it to POST requests, leading to a 404.
2. Incorrect Request Content Type or Parameter Binding
When using Postman, make sure you're sending the request in a format your API expects:
- If your method uses
[FromBody]to accept JSON data, set the Content-Type header toapplication/jsonand paste valid JSON in the request body. - If you're passing form data, use
application/x-www-form-urlencodedormultipart/form-datainstead. - Don't forget to add
[FromBody],[FromForm], or[FromQuery]attributes to your method parameters if needed—without these, ASP.NET might not know where to pull the data from, causing routing to fail.
3. Typos or Path Mismatches
It sounds silly, but even a small typo in your Postman request path can cause a 404. Check:
- The controller name in the path (e.g., if your controller is
ProductController, the route should includeapi/product). - Any route parameters (like
appKey) are included in the correct position (either in the URL path or query string, depending on your method's setup). - Case sensitivity—while ASP.NET Core is case-insensitive by default, some hosting environments or custom routing constraints might change this.
4. Missing Routing Middleware in Startup
Double-check your Program.cs (or Startup.cs) to ensure you've set up routing correctly. You need these two lines in your pipeline:
app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); // This maps attribute-routed controllers });
Without MapControllers(), your API won't register any controller routes—so POST (and all other) requests will return 404.
5. Enable Detailed Routing Logs
If you're still stuck, turn on debug logging for routing to see exactly what's happening when your request comes in. Update your appsettings.json:
{ "Logging": { "LogLevel": { "Microsoft.AspNetCore.Routing": "Debug" } } }
Run your app and send the POST request again—you'll see detailed logs in the console showing which routes ASP.NET tried to match, and why it couldn't find a match for your request. This is usually the fastest way to pinpoint the issue.
内容的提问来源于stack exchange,提问作者jleach

