alex
alex

Reputation: 7443

GET requests work but POST requests 404 in Web API 2

I've got a project which uses MVC 5 and Web API 2. Locally, both HTTP GET and POST requests to the Web API controller are working. When the website is published and deployed to an external environment, the GET requests still work, but the POST requests result in a 404.

Here's my API controller:

public class ExampleApiController : ApiController
{
    [HttpPost]
    [Route("GetRoles")]
    public IHttpActionResult GetRoles([FromBody] string userName)
    {
        // ...
        return Json(response);
    }

    [HttpGet]
    [Route("GetUsers")]
    public HttpResponseMessage GetUsers()
    {
        HttpResponseMessage response;
        // ...
        return response;
    }
}

Here's my WebApiConfig.cs file:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

        config.Routes.MapHttpRoute(
            name: "Web API default route",
            routeTemplate: "api/{controller}/{action}",
            defaults: null
        );
    }
}

Here's my Global.asax file:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

Some other notes:

Any ideas?

Upvotes: 2

Views: 1137

Answers (2)

alex
alex

Reputation: 7443

The issue was resolved - not super helpful for future readers, but the issue was related to a third-party security client on the server-side specifically blocking the request. Juliano's comment helped me in that direction of realizing this.

Upvotes: 0

Pankaj Rawat
Pankaj Rawat

Reputation: 4573

I would suggest use web API documentation (help page) or swagger to check your web api method actual url and parameter. you must missing something.

few day back I've face similar problem (post method working on local but not working on QA env.) but that time I was getting 405 error

Upvotes: -2

Related Questions