Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a private method, which can pass both GET and POST request, so I can all both post and get from one controller. I manage to create private method but to be able to access the GET parameters in the POST method, I need to create a Request object, which I am little unclear, what logic would i need to implement.

Please advice. Thanks in advance.

C#
[Authorize]
        [HttpPost]
      public HttpResponseMessage post([FromBody] Query query)
        {
            return getPata(request, query);
        }

        [Authorize]
        [HttpGet]
        public HttpResponseMessage get([FromUri] Query query)
        {
          return getPata(request, query);
        }

        private HttpResponseMessage getPata(typeofRequest request ,Query query)
        {
            if (User.IsInRole("admin"))
            {
                IQueryable<data_qy> Data = null;
				
                if (!string.IsNullOrEmpty(query.name))
                {
                    var ids = query.name.Split(',');

                    var dataMatchingTags = db.data_qy.Where(c => ids.Any(id => c.Name.Contains(id)));

                    if (Data == null)
                        Data = dataMatchingTags;
                    else
                        Data = Data.Union(dataMatchingTags);
                }

                if (Data == null) 
                    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);
            }

            return request.CreateErrorResponse(HttpStatusCode.NotFound, "Access Denied, Please try again.");
} 
}
Posted

1 solution

 
Share this answer
 

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