简体   繁体   English

为什么我的 C# API 在服务器上工作正常,但当我尝试从任何其他客户端计算机调用它时总是失败?

[英]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.我有一个 C# API,它将 Excel 工作表(文件路径,工作表名称)作为输入,并将工作表内容作为 JSON 表单中的 output 返回。 The API works fine when I try to test it on my machine that contains windows server 2016 installed on it.当我尝试在安装了 windows 服务器 2016 的机器上测试它时,API 工作正常。 But it always fail when I try to send the file path and sheet name from the form.但是当我尝试从表单发送文件路径和工作表名称时,它总是失败。 This is my API Code...这是我的 API 代码...

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..当我在服务器上测试 API 时,JSON 结果完美返回,但是当我尝试使用这种形式测试它时.. 在此处输入图像描述 It always returns 404 (Not Found).它总是返回 404(未找到)。

This is my sample excel sheet data.这是我的示例 excel 工作表数据。 在此处输入图像描述

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.问题全都与 IIS 的访问权限有关。当我尝试访问本地计算机或客户端计算机上的文件时,IIS 应用程序池必须对该位置具有访问权限。 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'右键单击文件夹和 select“属性”
  3. Under 'Security' Tab, select Edit.在“安全”选项卡下,select 编辑。
  4. In the 'Edit' Window select 'Add' -> 'Advanced' -> Find Now在'编辑' Window select '添加' -> '高级' -> 立即查找
  5. Look up the result names while you find the user related to IIS -[IIS-IUsers]- in my case.在找到与 IIS -[IIS-IUsers]- 在我的例子中相关的用户时查找结果名称。
  6. allow access controls to the folder -full access- or -Read and Write- etc...允许对文件夹的访问控制 - 完全访问 - 或 - 读取和写入 - 等等......

I Hope this is helpful for you.我希望这对你有帮助。 Thank you all.谢谢你们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我从C#调用另一个程序不起作用? - Why does my call to another program from C# not work? C#:客户端计算机是否需要安装SQL Server,同时连接到安装了SQL Server的其他计算机(服务器计算机) - C# :Does Client machine need SQL Server installed on it while connecting to other machine having SQL Server installed on it (the Server machine) C# 当我尝试在最终方法中调用变量中的值时,它们被设置为 0。它们在之前被调用时正确显示 - C# When I try to call the values from my variables in my final method they are set to 0. they show correctly when they are called previously 为什么C#DirectoryServices无法与我的LDAP服务器一起使用? - Why does C# DirectoryServices not work with my LDAP server? 如果我的客户端程序是Java而不是C#,为什么必须添加服务器SSL证书? - Why do I have to add the server SSL certificate if my client program is in Java and not when it is in C#? 为什么在运行计时器以回调该方法时脚本会挂起? Unity C# - Why Does My Script Hang When I Run The Timer To Call Back The Method ? Unity C# 我的SQL函数有什么问题,当我尝试在C#中检索其值时,它总是返回null? - What is wrong with my SQL function that it always returns null when I try to retrieve its value in C#? 任何想法为什么这不起作用? C# - Any ideas why this does not work? C# 为什么我的 C# Xml 代码仅在我枚举变量可枚举时才有效 - Why does my C# Xml code only work when I enumerate variable enumerable C#:UDP客户端无法从一台计算机运行到另一台计算机 - C#: UDP client not working from one machine to other
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM