[SOLVED] ASP.Net WepAPI – I can get results from Get Methods but I cannot Post

Issue

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?

Solution

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.

Answered By – Serge

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *