6

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.

8
  • Check you web config and see if POST requests are allowed Commented Jul 6, 2017 at 17:05
  • I found out that I get a 415 "Unsupported Media Type" if I change the content type to almost anything else than application/json (which gives the 404). But that still doesn't solve my problem. Commented Jul 7, 2017 at 6:04
  • Are you posting something the ModelBinder can bind to the InputModel class? Commented Jul 7, 2017 at 7:48
  • Yes, @RickvandenBosch. I copied the exact same literal input data between the queries. Commented Jul 10, 2017 at 6:48
  • I just created exactly this controller in a clean asp.net core 2.0 MVC API project. I then deployed it to a clean new app service via visual studio. Everything works. Not sure what is happening on your side. I can only imagine that it somehow relates to "It is an ASP.NET Core MVC application targeting the full .NET framework". Try running your controller standalone in a .net core web app, if this works then you could be a step closer. It might just be some routing error then. I never integrated a .net core 2.0 project into a .net framework application, so no idea about that unfortunately. Commented Nov 12, 2017 at 11:45

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It's been a while, but yes, your answer fit perfectly with what I experienced as far as I recall :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.