Access the .net web api from a different computer

Currently, I am working on a project that requires me to create a REST API for clients. This API will retrieve data from a database and return it in JSON format. Below is the code for my 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 issue I am facing is that I can only access the API from the same PC where the Web API is running. The link I use to reach it looks like this:

     my_ip:54780/users/parameters

When I call from the same PC, everything works fine. However, I cannot access it from another PC. I have tried enabling CORS in different ways, but none of them seem to work. First, I attempted:

  • Enabling CORS in webapiconfig.cs:

    var cors = new EnableCorsAttribute("*", "*", "*");
    config.EnableCors(cors);
    

This did not solve the issue.

  • Next, I added a new entry in web.config:

    <add name="Access-Control-Allow-Origin" value="*"/>
    

Unfortunately, this approach also did not work.

  • Lastly, I tried 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 tested using Postman from another PC, as well as XMLHttpRequest and AJAX, but none of these methods worked. There seems to be a 20-second delay, followed by an error response without any additional information.

If you have any suggestions on how I can resolve this access issue with the API, I would greatly appreciate it. The API will eventually be utilized by a mobile application, so it needs to support simple requests.

Thank you in advance for your assistance.

Answer №1

Based on your inquiry, it seems like you will have to configure the port on your Web API server. Specifically, you should open port 54780

Opening the port is a simple task, here is a link to a Microsoft page that provides detailed instructions: Open a port in Windows Firewall

  1. To begin, access Windows Firewall by navigating to Control Panel after clicking the Start button, searching for "firewall," and selecting Windows Firewall.

  2. In the left panel of the Windows Firewall with Advanced Security dialog box, select Advanced settings. If prompted for administrator permission, provide the necessary credentials.

  3. Select Inbound Rules from the left pane and then click New Rule in the right pane.

  4. Proceed with adding port 54780 to your rule by following the prompts in the New Inbound Rule wizard.

Note that if you are utilizing a third-party firewall such as part of your AntiVirus software, you will also need to open the port within that application.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with Vue.js array filtering feature in my program

My Vue.js code includes a search input with a dropdown menu populated by an API data array called categories. I am attempting to implement a filter on the search input so that when a value is typed, it filters the dropdown menu items based on the API data. ...

Developing a dynamic horizontal bar chart in d3 using a multidimensional array or nested JSON dataset

I am in the process of creating a straightforward d3 bar chart () by utilizing this specific nested json file. { "clustername": "cluster1", "children": [ { "neighborhoodname": "Shaw", "children": [ { "totpop2000": "1005", "children": [ ...

React: When component is suspended, the useEffect hook is not triggered

Exploring the world of react hooks and react suspense has led me to creating a custom hook called useApolloQuery. This hook is designed to fetch data and utilize a promise to wait until the data is loaded. My approach involves placing the data fetching lo ...

Hiding CheckBox items within a CheckBoxList control in ASP.NET

Currently, I am working with a CheckBoxList control that is bound to data and can potentially become very large. In order to manage this, I am implementing a filter mechanism. Here is my approach: foreach( ListItem item in this.UserRolesList.Items ) { ...

Comprehending the fundamentals of Matrix Operations

I've been struggling with basic matrix transformations and can't seem to grasp it. There's a plethora of outdated code online and I'm unsure of what's current. Here's the snippet I've been working with: var matrixInitia ...

Children can easily access multiple items within a list by using the ul tag

I am struggling with changing the class of ul inside the ul.navigator. I want to change it only when I click on a particular li, but my current approach doesn't seem to be working. Can anyone provide some guidance or assistance? $('.navigator& ...

Implementing OnSuccess with Ajax.BeginForm in MVC 3 Razor

I'm currently learning about MVC and I'm having trouble updating my page after submitting a form. My goal is to simply hide the form and display the contents of a div with OnSuccess. This is my code: <script type="text/javascript> $(d ...

Combining JSON and JavaScript for embedding a SPARQL query into an HTML document

I have successfully implemented an HTML + SPARQL + JSON + JavaScript program, which can be viewed here: Below is the code snippet for the SPARQL + JSON + JavaScript: function retrieveData() { var query = "PREFIX : <http://dbpedia.org/resource/> P ...

The npm outdated command seems to ignore the caret notation specified in the package.json file

When looking at a package.json file that contains the following: "devDependencies": { "grunt": "^0.4.5", "grunt-concurrent": "^1.0.0", "grunt-contrib-jshint": "^0.10.0", "grunt-contrib-watch": "^0.6.1", "grunt-dev-update": "^1.1.0", ...

Transforming an array of HTTP Observables into an Observable that can be piped asynchronously

I am attempting to execute a series of XHR GET requests based on the results of an initial request. I have an array of observables representing the secondary requests I wish to make, and I am able to utilize Array.map for iterating over them and subscribin ...

Transforming Word and Excel files into PDFs through ITEXTSHARP algorithm

Is it possible to convert Word and Excel documents to PDF using ITEXTSHARP in C# .Net without relying on Microsoft Interops? Any alternate methods available for achieving this task? Thanks in advance. ...

Discovering common elements in various arrays of objects

Details: record1 = [{"site": "The Blue Tiger", "zipcode": "E1 6QE"}, {"site": "Cafe Deluxe", "zipcode": "E6 5FD"}] record2 = [{"site": "Blue Tiger", "zi ...

Using Angularjs and PHP: incorporating URL parameters into the $http.get() function

I am currently working on a web project in Eclipse with three main files: "controller.js", "home.html," and "array.php". The goal is for the .php file to echo a PHP array encoded in a JSON string: <?php $arr = array(array(title => "hello", auth ...

Alternate routing based on conditions in Angular

I've used the "$urlRouterProvider.otherwise('{route here}')" syntax in angular to create a catch-all route in Angular UI-Router. One thing I'm curious about is whether it's possible to have conditional "otherwise" routing based o ...

What is the best way to extract a parameter from the DownloadDataAsync function?

In my WPF app, I have successfully used the WebClient to download images and trigger events when the downloads are complete. However, I am facing an issue when trying to pass parameters to the ImageDownloadCompleted method to identify which image was downl ...

Optimal approach for merging requests that may have varying return types

Context I am currently developing a web application that utilizes AJAX to fetch content and data in a typical manner. There are specific events that may occur on the server-side which need to be communicated to the client-side JavaScript framework, and v ...

Struggling with parsing a JSON array of objects?

{ "-6M7BoTyaAplYy120JAi": { "Address": "sample", "ID": "sample", "Name": "sample", "PhoneNumber": "sample" }, "-6M7Bp ...

Why doesn't Select2 function properly in Bootstrap 3 when the tabindex is removed?

There is a bug with Select2 that causes it to malfunction in a Bootstrap 3 modal unless the tabindex element is removed. I have successfully resolved this issue in several modals on my page, but one specific modal still cannot get Select2 to function. Eac ...

Although my ajax request was unsuccessful, I was able to successfully upload my post

Why is my ajax request failing while still uploading my post? How can I prevent this failure? Here is my ajax request code: createRate(){ var ourNewPost = { 'title': $(".new-rate-title").val(), 'content': $('. ...

Attempting to establish a connection with a MySQL database through the utilization of Node.js in combination with the mysql javascript client

Recently, I've been facing a challenge connecting to a MySQL database from my electron app: <script> var mysql = require('mysql'); var connection = mysql.createConnection({ host : '*********', ...