Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core Web API HTTP POST returns 404 in Azure

I have this (simplified) ASP.NET Core Web API controller. Both the GET and POST actions works nicely on my own machine. However, deployed to Azure only the GET action works properly. The POST action results in a 404. Any ideas?

namespace Foo
{
  using System;
  using System.Collections.Generic;
  using Microsoft.AspNetCore.Mvc;

  [RequireHttps]
  [Produces("application/json")]
  [Area("Foo")]
  [Route("[area]/Api/[controller]")]
  public class BarController : Controller
  {
    [HttpGet]
    public IEnumerable<string> Get()
    {
      return new[] {"Hello", "World!"};
    }

    [HttpPost]
    public void Post([FromBody] InputModel model)
    {
    }

    public class InputModel
    {
      public int Foo { get; set; }
    }
  }
}

It is an ASP.NET Core MVC application targeting the full .NET framework. It is deployed as an Azure Web App. I have tested both actions on my local machine and in Azure using Postman.

like image 907
Bjarke M Avatar asked Jul 06 '17 16:07

Bjarke M


1 Answers

It seems under certain circumstances when an error occurs in a controller, ASP.NET core returns 404 instead of a more appropriate 500. Check out this post:

ASP.NET Core 2.0 site POST request resulting in 404 on Azure

In the answer, it was caused by wrong database connection string (which may be a case when you publish an application to another environment), but it may be something else.

Not sure why it does not throw a normal error, maybe it tries to redirect to a custom error page or something like that.

To check this suggestion, try to remove everything from your POST controller to isolate a problem. If it won't return 404, it means that something in the controller throws an exception.

like image 136
Andrew Simontsev Avatar answered Nov 15 '22 04:11

Andrew Simontsev