简体   繁体   中英

Why does my C# API work fine on the server but it always fails when I try to call it from any other client machine?

I am having a C# API that takes an Excel sheet (filePath, sheetName) as an input and return the sheet content as the output in JSON form. The API works fine when I try to test it on my machine that contains windows server 2016 installed on it. But it always fail when I try to send the file path and sheet name from the form. This is my API Code...

public IHttpActionResult GetSheetData(string filePath, string sheetName)
        {
            try
            {
                var connectionString = $@"
                                        Provider=Microsoft.ACE.OLEDB.12.0;
                                        Data Source={filePath};
                                        Extended Properties=""Excel 12.0 Xml;HDR=YES""";
                using (var conn = new OleDbConnection(connectionString))
                {
                    conn.Open();
                    var cmd = conn.CreateCommand();
                    cmd.CommandText = $@"SELECT * FROM [{sheetName}$]";
                    using (var DRow = cmd.ExecuteReader())
                    {
                        var query = DRow.Cast<DbDataRecord>().Select(row => new
                        {
                            name = row[0],
                            age = row[1],
                            email = row[2]
                        });
                        var JSON = JsonConvert.SerializeObject(query);
                        return Ok(JSON);
                    }
                }
            }
            catch (Exception) { return Ok(HttpStatusCode.NotFound); };
        }

The JSON result is returned perfectly when I test the API on the Server, But when I try to test it using this form.. 在此处输入图像描述 It always returns 404 (Not Found).

This is my sample excel sheet data. 在此处输入图像描述

Any Ideas?!

I have found the solution to my Issue, and I wanted to share with you.

The Issue was all about the access rights of the IIS. When I try to access a file on my local machine or in the clients machine, the IIS App Pool must have an access rights on that location. So, I used the following steps.

  1. Browse to the folder containing the file -to be read-
  2. Right Click the folder and select 'Properties'
  3. Under 'Security' Tab, select Edit.
  4. In the 'Edit' Window select 'Add' -> 'Advanced' -> Find Now
  5. Look up the result names while you find the user related to IIS -[IIS-IUsers]- in my case.
  6. allow access controls to the folder -full access- or -Read and Write- etc...

I Hope this is helpful for you. Thank you all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM