1

My API contains 2 Get methods and 1 POST. The 2 Gets work however the POST returns this messgae:

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

These are my methods:

[HttpGet]
    public IEnumerable<tblMetrHist> Get(string accntnum)
    {
        ...
    }
    [HttpGet]
    public IEnumerable<CustomerInfo> GetCustomer(string accntnum)
    {
        ...
    }
    [HttpPost]
    public IHttpActionResult Post([FromUri] string num, [FromUri] string reading)
    {
        using (CustomerEntities entities = new CustomerEntities())
        {
            entities.tbl1.Add(new tbl1()
            {
                Number = num,
                Reading = reading
            });

            entities.SaveChanges();
        }

        return Ok();
    }

And my Route is simply:

config.Routes.MapHttpRoute(
                name: "DefaultApiWithAction",
                routeTemplate: "api/{controller}/{action}"
            );

So I'm not sure how to make the API recognize the method as a "Post" and not a "Get". Help please?

Serge
  • 40,935
  • 4
  • 18
  • 45
Kristin Vernon
  • 155
  • 1
  • 2
  • 20
  • What url you were using for get and for post? And what version MVC you are using? – Serge Jul 28 '21 at 00:09
  • Are you posting your data using HTTP url or HTTPS url ? Try below links.https://stackoverflow.com/questions/40640317/webapi-post-methods-always-returns-the-requested-resource-does-not-support-http – Chinmay T Jul 28 '21 at 00:12
  • https://stackoverflow.com/questions/35065385/asp-net-webapi-405-method-not-allowed?answertab=votes#tab-top – Chinmay T Jul 28 '21 at 00:12
  • @Serge MVC is: newVersion="5.2.7.0" as for the urls they are https..api/Data/Get?num={num}, https..api/Data/GetCustomer?num={num}, and https..api/Data/Post?num={num}&reading={reading}. Like I said the two Gets work but not the Post, it gives me the message I put in my OP – Kristin Vernon Jul 28 '21 at 00:29
  • @ChinmayT always in https – Kristin Vernon Jul 28 '21 at 00:33
  • @KristinVernon Thanks, did you try my code?With your urls post should work now. pls let me know – Serge Jul 28 '21 at 00:38

1 Answers1

0

create ViewModel

public class ViewModel
{
 public string Num {get; set;}
 public string Reading {get; set;}
}

and try this

[HttpPost()]
 public IHttpActionResult Post([FromUri] ViewModel model)
  {
       var num=model.Num;
        .....
}

UPDATE

Since the browser automatically performs a get call, I recommended PO to use another tool to post. "Advanced Rest Client" of Chrome was selected by PO to make a post and everytning worked properly.

Serge
  • 40,935
  • 4
  • 18
  • 45