Click here to Skip to main content
15,868,292 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to change the following api controller method from GET to POST request. I have made the changes on the header however I am getting a the following error, whenever I call api/list?tag=89077&name=prest :

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

C#
[HttpPost]
        public HttpResponseMessage Post([FromBody] Query query)
        {
	          IQueryable<data_qy> Data = null;

                 if (Data == null) // If no tags or name is being queried, apply filters to the whole set of products
                    Data = db.data_qy;

                if (query.endDate != null)
                {
                    Data = Data.Where(c => c.UploadDate <= query.endDate);
                }

                if (query.startDate != null)
                {
                    Data = Data.Where(c => c.UploadDate >= query.startDate);
                }

                Data = Data.OrderByDescending(c => c.UploadDate);

                var data = Data.ToList();

                if (!data.Any())
                {
                    var message = string.Format("No data found");
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
                }

                return Request.CreateResponse(HttpStatusCode.OK, data);
            }


*Edit
C#
public class Query
{
    public string name { get; set; }
    public string tag { get; set; }
    public Nullable<DateTime> startDate { get; set; }
    public Nullable<DateTime> endDate { get; set; }

}


JavaScript
$(document).ready(function () {

    var data = {name: "", tag: ""};

    var url = '/api/list/Post';
    $.ajax({
        url: url,
        data: JSON.stringify(data),
        type: 'POST',
        contentType: "application/json",
    });

   
});


I am little unsure, if the (data), should be calling data variable in the javascript or calling query object. I also would like to ask, I can test POST method as I test GET methods, such as api/list/name=pres&tag=679.

I have tested the above code, by passing querystring as -- api/list/name=pres&tag=679. However, it still gives me the same error.

Please advice, if I am missing something or if I am using an incorrect approach.

Do I need to change code in my global.aspx or somewhere else. Please advise.

Many thanks.
Posted
Updated 4-Jun-14 4:22am
v2

I had same problem. Can you try below link and see if it does work for you

http://stackoverflow.com/questions/12765636/the-requested-resource-does-not-support-http-method-get[^]
 
Share this answer
 
Comments
miss786 4-Jun-14 10:20am    
Apology for the late response. I was unable to solve the issue using the above link. However I have added a ajax code on the client end and i am little unsure, if its the correct logic and syntax, as the query is still throwing the same error as an output response. Please advice. Thank you for your time and help.
you can pass multiple parameters like that in post method.. :)

JavaScript
var tagValue=""; //What ever you like
var NameValue="" //What ever you like


    var url = 'PageName/MethodName';
    $.ajax({
        url: url,
        data:"{'tag':'"+tagValue+"','name':'"+NameValue+"'}"
        type: 'POST',
        contentType: "application/json",
        success:function(data)
        {
           alert("success");
        },
        error:function(result)
        {
           alert("error");
        }
    });
 
Share this answer
 
v2
Comments
miss786 5-Jun-14 8:43am    
Thank you for your response. I tried your code but I am sorry to inform, I still experiencing the same error as mentioned above. I would like to ask, can you call post method as q query string --> api/test?name=bacm.

any help much appreciated.

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