I would like to be able to call GET and POST request from the same url (api/test...), but I am currently getting current context error on the following line of code:
[AcceptVerbs(HttpVerbs.Get|HttpVerbs.Post)]
//The name 'HttpVerbs' does not exist in the current context -- error.
I have added in the MVC namespace, but since my application is web api, its causing the authorize property to fall through when declaring the mvc namespace.
[Authorize]
[AcceptVerbs(HttpVerbs.Get|HttpVerbs.Post)] // error
public HttpResponseMessage post([FromUri] 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.data_qy != null)
{
Data = Data.Where(c => c.UploadDate >= query.data_qy);
}
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.");
}
}
Any advice would be very much appreciated. Many thanks.
Instead of writing:
[AcceptVerbs(HttpVerbs.Get|HttpVerbs.Post)]
have you tried:
[AcceptVerbs("GET","POST")]
or, even better:
[HttpGet]
[HttpPost]
?
The HttpVerbs
class (in namespace System.Web.Mvc
) is specific to MVC projects, not Web API.