1

I can't seem to reach an endpoint using the POST method but the very same endpoint works fine when using the endpoint as a GET method. I have mentioned two endpoints below, one of which is a GET method and the other one is POST method.

The GET endpoint which is working fine looks like this:

    [Route("api2.0/Checklist/Create")]
    [HttpGet]
    public void Create()
    {
        JsonResponse.NewResponse("METHOD HIT!");
    }

The response I get in postman when I run endpoint above using the URL GET: http://localhost:10001/api2.0/Checklist/Create/ is:

{ 
    "Success": true, 
    "Message": "METHOD HIT!", 
    "RedirectLink": null, 
    "ErrorType": 0, 
    "Payload": null 
}

BUT when I run the same endpoint with a POST method from Postman using POST: http://localhost:10001/api2.0/Checklist/Create/ on this endpoint:

    [Route("api2.0/Checklist/Create")]
    [HttpPost]
    public void Create()
    {
        JsonResponse.NewResponse("METHOD HIT!");
    }

The response I'm getting is:

HTTP Error 404.0 - Not Found

My routing config looks something like this:

config.Routes.MapHttpRoute(
                name: "ChecklistController",
                routeTemplate: "api2.0/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

I also tried changing the post method URL to https from http but that doesn't seem to work either.

Any help would be appreciated! Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Luky
  • 11
  • 1
  • I just create a test project. I added a controller with two methods called `[HttpGet] void CreateOne()` and `[HttpPost] void CreateTwo()` (you can't create duplicate methods) with `[Route("api2.0/Checklist/Create")]` decorator and they worked. – Pablo Claus Jul 11 '22 at 19:21
  • You can [check this post](https://stackoverflow.com/questions/9552761/get-and-post-methods-with-the-same-action-name-in-the-same-controller#:~:text=To%20answer%20your%20specific%20question,doesn't%20distinguish%20the%20methods.)! Hope this helps you with your understanding. – Abhay Jul 19 '22 at 13:03

1 Answers1

0

I am amazed how you can compile your code since you cannot have two methods with the same name and input parameters in one class (in the same controller in your case).

according to you config file, "create" will {id} this is why it causess the error. But in any case you will have to fix the route, making it starting from the root

    [HttpPost("~/api2.0/Checklist/Create")]
     public void Create()
Serge
  • 40,935
  • 4
  • 18
  • 45