Search code examples
javascriptc#ajaxasp.net-apicontroller

Reach .net web api from another pc


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.


Solution

  • From your question, it appears as though you will need to open the port on your Web API machine. In your case you need to open port 54780

    It is quite easy to open your port, here Microsoft page with instructions on how to do it: Open a port in Windows Firewall

    1. Open Windows Firewall by clicking the Start button Picture of the Start button, and then clicking Control Panel. In the search box, type firewall, and then click Windows Firewall.

    2. In the left pane, click Advanced settings. Administrator permission required If you're prompted for an administrator password or confirmation, type the password or provide confirmation.

    3. In the Windows Firewall with Advanced Security dialog box, in the left pane, click Inbound Rules, and then, in the right pane, click New Rule.

    4. Follow the instructions in the New Inbound Rule wizard and add port 54780 to your rule.

    Please note, that if you are using a third party firewall e.g. with your AntiVirus software you will need to open the port in that application.