c# - 无法连接到 Controller 中的方法

标签 c# asp.net asp.net-web-api

<分区>

我正在使用 asp.net web api。我正在尝试从服务器获得肯定的响应,但是程序没有看到 Controller 中的一种方法。它进入 Controller 构造函数,但不会更进一步。 响应是

Status Code 404, Reason Phrase: Not Found.

我知道这可能是方法名称/参数/其他方面的拼写错误,但对我来说一切似乎都很好。我可能忽略了一些事情。

而且 API 工作正常,我已经用不同的方法进行了尝试。

Controller 方法如下:

    [HttpGet]
    [Route("api/ClientFiles/GetScanStatus")]
    [ActionName("GetScanStatus")]
    private Tuple<bool, bool?, bool?> GetScanStatus(int scanTypeId, int clientId, int inventoryId)
    {
        //businessLogic here 


        return new Tuple<bool, bool?, bool?>(false, null, null);
    }

调用 Controller 的方法:

  public static async Task<Tuple<bool, bool?, bool?>> GetScanStatus(int scanTypeId, int clientId, int inventoryId)
  {
            using (HttpClient client = new HttpClient())
        {
            client.Timeout = new TimeSpan(0, 0, 90);
            HttpResponseMessage response = await client.GetAsync($"{ConnectionURL}api/ClientFiles/GetScanStatus/?scanTypeId={scanTypeId}&clientId={clientId}&inventoryId={inventoryId}").ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsAsync<Tuple<bool, bool?, bool?>>();
                return result;
            }
        }

        return null;
    }

response.RequestedUri

http://localhost:61372/api/ClientFiles/GetScanStatus/?scanTypeId=26&clientId=12&inventoryId=25482

最佳答案

从您的代码看来,您的 Controller 方法是private,因此不可见!

使方法公开

[HttpGet]
[Route("api/ClientFiles/GetScanStatus")]
[ActionName("GetScanStatus")]
public Tuple<bool, bool?, bool?> GetScanStatus(int scanTypeId, int clientId, int inventoryId) {
    //...
}

我认为它会起作用。

关于c# - 无法连接到 Controller 中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55868582/

相关文章:

java - 是否有超过一个的帖子增量?

javascript - 无法创建 'System.Boolean' 类型的对象

c# - 从 ASP.NET MVC 站点中删除数据库引用

c# - 将路由属性添加到 ApiController

c# - 在 C# 中过滤巨大列表的最高效方法?

c# - 在 CQRS 模式中,应该在域服务或命令处理程序中工作

c# - c# 中的 AVG 查询

asp.net - 如何在不拆分单词的情况下包装从数据库中获取的文本?

c# - ASP .net Web Api 下载和查看图片

asp.net-web-api - WebAPI 请求在同一请求上抛出 404 错误...