I'm trying to build a simple Nancy self host. I got it working if I send in the proper path, but if I send empty path after port #, I get is error 404. If I send an invalid path I get error 500. What I want is to have a catch-all Get which is used whenever a request is sent with an invalid path.
Here is my program.cs
using System.Diagnostics;
using Nancy;
using Nancy.Hosting.Self;
namespace NancyDataService
{
class Program
{
static void Main(string[] args)
{
var uri = new Uri("http://localhost:8080");
var hostConfig = new HostConfiguration();
hostConfig.UrlReservations.CreateAutomatically = true;
hostConfig.RewriteLocalhost = false;
using (var nancyHost = new NancyHost(uri, new DefaultNancyBootstrapper(), hostConfig))
{
nancyHost.Start();
Console.WriteLine("Nancy now listening on http://localhost:8080. Press enter to stop");
try
{
Process.Start("http://localhost:8080");
}
catch (Exception)
{
}
Console.ReadKey();
}
Console.WriteLine("Stopped. Good bye!");
}
}
}
Here is my main module:
using Nancy;
namespace NancyDataService
{
public class MainModule: NancyModule
{
public MainModule()
{
string json_error = @"{""status"":""fail"",""reason"":""{0}""}";
string json;
Get("test", parms =>
{
return "test";
});
// this is default if desired path not sent
Get("{parm1}", parms =>
{
json = string.Format(json_error, "Invalid method name supplied");
//return (Response)json;
return json;
});
}
}
}
I've changed the Get syntax to match the Nancy 2.0 way. I was expecting that the last Get in the above code would be processed, and give me a default error message. If I enter http://localhost:8080/ in browser, I get error 404 response. If I enter http://localhost:8080/test it works fine. If I enter http://localhost:8080/anythingElse I get Error 500, Internal Server Error.
What I would like is to have a "default" get section so any unexpected path entered (including no path at all) after port #, it would take that branch.
BTW, this is targeting .Net Core 3.0, which Nancy says may not work. The warning in my Nancy.Hosting.Self package has a warning which says it was restored using .Net Framework (4.6.1 - 4.8). Could that be the issue?
Any ideas how to make that work? Thanks...
Solved most issues. Need
Get("/", ...for empty path, andGet("/{parm}",...for bad path. Seemsstring.Formatcauses error 500 butjson_error.Replace("{0}", "new text")works fine. What's up with that?