'nanoFramework webserver not recognizing routes with attributes
I have set up an ESP32-WROVER as a web server using the nanoFramework.WebServer library. It fires the route method when I use a route without attributes (//192.168.1.8/led) but not when I use a route with attributes (//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);
}
}
}
Case: //192.168.1.8/led Debug output: Handling request led
Case: //192.168.1.8/led/on Debug output:
Solution 1:[1]
That looks like a bug. Please raise an issue in our GitHub here.
Make sure to add the sample code above so it's easier to reproduce. Thanks!
Solution 2:[2]
Probably need to decorate additional attributes
[Route("led"), Route("led/on"), Route("led/off")]
public void Led(WebServerEventArgs e)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | José Simões |
Solution 2 | Sergey1380528 |