Alex Kibler
Alex Kibler

Reputation: 4954

MVC POST is 404ing but if I change the POST to a GET, it works

I have an MVC 4 application running on Kentico 11. I'm trying to make a new API endpoint for a node application to be able to hit it.

The method is structured as follow:

    [Route("api/SendEmail/projectEmail"), HttpPost]
    [EnableCors(origins: "http://localhost:3000", headers: "*", methods: "*")]
    public JsonResult KioskEmailSubscribe(KioskEmailModel emailModel)
    {
           // code that doesn't matter because we don't get this far
    }

Whenever I use Postman to make a POST to this endpoint, it 404s. However, when I change the HttpPost to a GET (and remove the emailModel), it goes through just fine.

I tried changing it back to a POST (still without the emailModel) and it still 404s. There's nothing fishy in my routeConfig as far as I know but I've attached it just to be safe:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.LowercaseUrls = true;

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("cms/{*pathinfo}");

        routes.Kentico().MapRoutes();

        routes.MapMvcAttributeRoutes();

        // Catch-all routes (Anything not matching a specific route pattern will route to the default controller and be determined there)
        routes.MapRoute("Default", "{*route}", new { controller = "Default", action = "Index" });
    }
}

Upvotes: 1

Views: 112

Answers (1)

Allen Chen
Allen Chen

Reputation: 226

I guess you didn't set JsonRequestBehavior.AllowGet in your return. The default setting of JsonResult doesn't allow client to send request with HTTP GET for system protection.

If you'd like to use HTTP GET, just make your return like this:

return Json("", JsonRequestBehavior.AllowGet);

For more details and discussion, you can refer to: Why is JsonRequestBehavior needed?

Upvotes: 1

Related Questions