【问题标题】:Unable to run the web API无法运行 Web API
【发布时间】:2020-05-11 08:45:10
【问题描述】:

我有两个web-apis

 public string GetRData(string data)
    {
        var serialPort = new SerialPort("COM1", 9600, Parity.Even, 8, StopBits.One);

        try
        {
            if(serialPort.IsOpen)
            {
                var stream = new SerialStream(serialPort);
                stream.ReadTimeout = 2000;

                //byte[] bytestoread = new byte[9999];
                byte[] bytestosend = StringToByteArray(data);

                serialPort.Write(bytestosend, 0, bytestosend.Length);
                //receiveData = serialPort.ReadExisting();

                serialPort.Close();
            }
            else
            {
                //serial port settings and opening it
                serialPort.Open();
                var stream = new SerialStream(serialPort);
                stream.ReadTimeout = 2000;

                //byte[] bytestoread = new byte[9999];
                byte[] bytestosend = StringToByteArray(data);

                serialPort.Write(bytestosend, 0, bytestosend.Length);
                //receiveData = serialPort.ReadExisting();

                serialPort.Close();
            }                         

        }
        catch(Exception ex)
        {

        }


        return data;
    }

    public string GetPData(string id)
    {
        var serialPort = new SerialPort("COM2", 9600, Parity.Even, 8, StopBits.One);
        string receiveData = "";
        try
        {
            //serial port settings and opening it
            serialPort.Open();

            var stream = new SerialStream(serialPort);
            stream.ReadTimeout = 2000;

            //byte[] bytestoread = new byte[9999];
            //byte[] bytestosend = StringToByteArray(data);

            //serialPort.Write(bytestosend, 0, bytestosend.Length);
            receiveData = serialPort.ReadExisting();

            serialPort.Close();


        }
        catch (Exception ex)
        {

        }


        return receiveData;
    }

WebApiConfig.cs

 config.Routes.MapHttpRoute(
       name: "GetRData",
       routeTemplate: "api/{controller}/{action}/{data}",
       defaults: null

       );

        config.Routes.MapHttpRoute(
      name: "GetPData",
      routeTemplate: "api/{controller}/{action}/{id}",
      defaults: new { id = RouteParameter.Optional }

      );

每当我尝试点击GetPData 时,我都会在Postman 中得到以下响应

{ "Message": "没有找到与请求 URI 'https://localhost:44337/api/rtu/GetPData/1' 匹配的 HTTP 资源。", "MessageDetail": "在控制器 'RTU' 上找不到与请求匹配的操作。" }

我已搜索过此问题,但无法找到解决方案。

注意:GetRData 工作正常

【问题讨论】:

    标签: asp.net-web-api2 http-status-code-405 webapi


    【解决方案1】:

    您将操作包含在 URI 中,而不是依赖于路由匹配的约定,因此您应该添加属性以明确指定允许的 HTTP 动词:

    [HttpGet]
    public string GetPData(string id)
    

    话虽如此,您的动作确实以“Get”开头,所以这应该是它匹配的动词。您应该确保请求是“GET”。否则路由看起来没问题。您是否尝试过使用 URL:

    https://localhost:44337/api/rtu/GetPData?id=1

    【讨论】:

    • 通过https://localhost:44337/api/rtu/GetPData?id=1 我得到value 作为回报。
    • 这就是serialPort.ReadExisting()receiveData 设置为(查看您发布的代码)?如果是这样并且您收到 HTTP 200 响应,那么看起来方法正在被命中...?
    • 不,它返回一个字节数组
    • 使用默认编码器 (ASCII) 解码为字符串的字节数组。当您说它返回value 时,您的意思是响应中的字符串“值”吗?即方法调用成功?
    • 好的,我不知道刚刚发生了什么。我清理解决方案,然后构建它。然后我运行它并调用 API 现在我得到 Multiple actions were found that match the request:
    猜你喜欢
    • 2014-01-07
    • 2021-10-21
    • 2022-01-09
    • 2018-02-18
    • 2022-09-27
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多