[英]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.
所以,我使用了以下步骤。
I Hope this is helpful for you.我希望这对你有帮助。 Thank you all.
谢谢你们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.