0
votes

I have two 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 }

      );

Whenever I am trying to hit the GetPData I am getting below response in Postman

{ "Message": "No HTTP resource was found that matches the request URI 'https://localhost:44337/api/rtu/GetPData/1'.", "MessageDetail": "No action was found on the controller 'RTU' that matches the request." }

I have searched for this issue but unable to get a solution.

Note: The GetRData is working perfectly

1

1 Answers

0
votes

You are including the action in the URI rather than relying on convention for route matching so you should add attributes to explicitly specify the allowed HTTP verbs:

[HttpGet]
public string GetPData(string id)

Having said that your action does start with "Get" so that should be the verb to which it matches. You should ensure that the request is a "GET". Otherwise the routing looks OK. Have you tried hitting it with the URL:

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