0

我使用 nanoFramework.WebServer 库将 ESP32-WROVER 设置为 Web 服务器。当我使用没有属性的路由(//192.168.1.8/led)时它会触发路由方法,但当我使用有属性的路由(//192.168.1.8/led/on)时不会触发

using (WebServer server = new WebServer(80, HttpProtocol.Http, new Type[] { typeof(ControllerGpio) }))
{
    // Start the server.
    server.Start();

    Thread.Sleep(Timeout.Infinite);
}
class ControllerGpio
{
    private static GpioController _gpioController = new GpioController();

    private static int ledPin = 2;

    [Route("led")]
    public void Led(WebServerEventArgs e)
    {
        try
        {
            var rawUrl = e.Context.Request.RawUrl.TrimStart('/');
            Debug.WriteLine($"Handling request {rawUrl}");
            var args = rawUrl.Split('/');
            if (args.Length < 2)
            {
                WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.BadRequest);
                return;
            }

            // If the pin is not open
            if (!_gpioController.IsPinOpen(ledPin))
            {
                // Open pin
                _gpioController.OpenPin(ledPin);
                // Set pin mode to output
                _gpioController.SetPinMode(ledPin, PinMode.Output);
            }

            if (args[1].ToLower() == "on")
            {
                _gpioController.Write(ledPin, true);
            }
            else if (args[1].ToLower() == "off")
            {
                _gpioController.Write(ledPin, false);
            }
            else
            {
                WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.BadRequest);
                return;
            }

            WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.OK);
        }
        catch (Exception)
        {
            WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.BadRequest);
        }
    }
}

案例://192.168.1.8/led 调试输出:处理请求 led

案例://192.168.1.8/led/on 调试输出:

4

1 回答 1

0

这看起来像一个错误。请在我们的 GitHub 上提出问题

确保添加上面的示例代码,以便更容易重现。谢谢!

于 2021-08-25T15:19:16.617 回答