Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I'm trying to make an API in ASP.NET Framework 4.6.1 (I know) and I'm having trouble at POST requests. I'm using Postman for testing. After I configure a post request and send it to the server and send it I recieved the error :

{
    "Message": "The requested resource does not support http method 'POST'."
}


Get Method works fine.

I uploaded a test project with the same behavior (PostMethod doesn't work but get method works) here :

https://github.com/gabrielbalinca/WebApiTestNetFramework


My code :

TestController :
// GET Method
public IHttpActionResult GetDetails()
{
   return Ok("Test from visual studiop");
}

// POST Method
public IHttpActionResult PostDetails(string message)
{
    return Ok(message);
}


WebApiConfig :
public static void Register(HttpConfiguration config)
{
   // Web API configuration and services

   // Web API routes
   config.MapHttpAttributeRoutes();

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


Here is a photo of Post request on postman :

Imgur[^]

What I have tried:

I've tryed to put tags like [HttpPost] and [Route("api_path")] like that :
// GET Method
[HttpGet]
[Route("api/Licensing/Details")]
public IHttpActionResult GetDetails()
{
   return Ok("Test from visual studiop");
}

// POST Method
[HttpPost]
[Route("api/Licensing/Details")]
public IHttpActionResult PostDetails(string message)
{
    return Ok(message);
}
Posted
Updated 5-Jul-23 1:05am
v5

I have not done FrameWork Web API for a while. However... How does Asp.Net match this:
JavaScript
"Message": "The requested resource does not support http method 'POST'."

to this?
C#
public IHttpActionResult PostString(string l)

Try changing to:
C#
public IHttpActionResult PostString(string message)

UPDATE

I fired up a test project. I set a breakpoint inside the PostString (POST) method. I hit it, and as mentioned above, the method is hit but param l is null. If I change param l to message, I can now see the value passed, in this case:
The requested resource does not support http method 'POST'."


UPDATE

When passing any (JSON in this case) body data as parameters with the inherited ApiController, you need to specify the [FromBody] attribute for each parameter.

For Attribute Routing, use this:

1. WebApiConfig - comment out the routing.
C#
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

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

2. Add a TestController class
C#
[RoutePrefix("api/test")]
public class TestController : ApiController
{
    [HttpPost]
    [Route("poststring")]
    public IHttpActionResult PostString([FromBody] string message)
    {
        return Ok(message);
    }
}

3. Run PostMan and point to: https://localhost:44344/api/test/poststring

For Normal routing, use the following:

1. WebApiConfig - set routing template
C#
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

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

2. TestController
C#
{
public class TestController : ApiController
{
    [HttpPost]
    public IHttpActionResult PostString([FromBody] string message)
    {
        return Ok(message);
    }
}

3. Run PostMan and point to: https://localhost:44344/api/test/poststring
 
Share this answer
 
v5
Comments
Gaby94 5-Jul-23 3:34am    
Still doesn't work. Any other sugestion ?
Graeme_Grant 5-Jul-23 3:45am    
See my update. If you are not hitting thr breakpoint, then you are not using the correct URL in PostMan. Also, check your message body in postman, make sure that it is set to JSON, not the default TEXT.

Here is my Postman settings:

URL: [POST] https://localhost:44344/home/PostString
Body [raw, JSON]
{
    "Message": "The requested resource does not support http method 'POST'."
}
Gaby94 5-Jul-23 3:55am    
that is strange acctually, should't be https://localhost:44344/api/home/PostString ?
Graeme_Grant 5-Jul-23 3:57am    
That is MY url, set yours as needed. I do not know the port address for your app, you need to check that too.
Gaby94 5-Jul-23 5:39am    
I made a new project, still the same issue, get mothod works, post doesn't.
Before I will look at the code as the culprit, I will check the following first -
1) The route configuration in your 'WebApiConfig' class is set up to handle POST requests. You have defined your route as 'api/{controller}/{id}'. Make sure that your controller 'TestController' is correctly mapped to this route.
2) For a POST request, the action should be named Post followed by the name of your resource. In your case, the action is named 'PostString', which might be causing the issue. You can rename it to 'PostObject' to match the resource name (Best Practices for Naming REST API Endpoints[^] or 5 essential HTTP methods in RESTful API development[^]
The Web API would expect a POST request to be handled by an action that you named 'PostString'. According to the naming convention, it should be named 'PostObject' since you are expecting a string object in the request body.

With above in mind, I will use the following code (not tested) -
C#
// POST Method
[HttpPost]
public IHttpActionResult PostObject(string l)
{
   Debug.WriteLine($"Received from client: {l}");
   if (!ModelState.IsValid)
      return BadRequest("Invalid data");
   return Ok(l);
}


Remember to update your request URL in Postman to match the updated action name (/api/Test/PostObject) when testing the POST request.
 
Share this answer
 
Comments
Gaby94 5-Jul-23 7:06am    
I updated my question with more details.
Graeme_Grant 5-Jul-23 7:46am    
I've identified your issue ... Check my updated solution, I give 2 x solutions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900