I'm working on a project, where I need to provide a REST API for clients, which will return some data form a database in JSON format. 'Till now here is my code for the controller:
[Models.AllowCors]
public HttpResponseMessage Get(string Id)
{
string ClearName = Id.Replace("_", " ");
IQueryable<Models.User> userQuery =
from user in Models.TableAccesser.Users_Table where
user.Name == ClearName
select user;
return Request.CreateResponse(HttpStatusCode.OK, userQuery);
}
The problem I encountered is that I can reach the api only from the same pc as where the web api runs. I can reach via a link like this:
my_ip:54780/users/parameters
If I call from the same pc, it works fine, but I can't reach it from another pc. I tried allowing cors in a few ways, but neither worked. I tried:
Enabling cors in webapiconfig.cs:
var cors = new EnableCorsAttribute("*", "*", "*"); config.EnableCors(cors);
It didn't worked
then I tried adding a new entry in web.config:
<add name="Access-Control-Allow-Origin" value="*"/>
this worked neither
then the last thing I tried was adding a function:
public class AllowCors : ActionFilterAttribute { public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { if (actionExecutedContext == null) { throw new ArgumentException("ActionExecutedContext"); } else { actionExecutedContext.Response.Headers.Remove("Access-Control-Allow-Origin"); actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*"); } base.OnActionExecuted(actionExecutedContext); } }
I tried using postman from another pc, and xmlhttprequest, ajax, but neither worked. there is a delay for around 20 seconds, then nothing, if I try to write out the response.responseText, it's just an "error", nothing more.
Can you please give any ideas what can I try to access the API? This API will be used from a mobile application, so it should work with simple requests.
Thank you in advice for your responses.