Nancy Self Hosting - URL not found

Published on 25 November 2017

Recently, I tried using Nancy in a self hosted environment. I have been exploring it for a way to capture token from IdentityServer login flow into a desktop app. My thought process was to use it as post logout URL provider and capture the token provided.

Overall, what I was trying was a simple thing with a host and a NacnyModule subclass. Something like following:

class Program
{
    static void Main(string[] args)
    {
        var hostConfig = new HostConfiguration
        {
            RewriteLocalhost = false
        };
        var host = new NancyHost(hostConfig, new Uri("http://localhost:5050"));
        host.Start();
        Console.ReadKey();
        host.Stop();
    }
}


class NancyLoginModule : NancyModule
{
    public NancyLoginModule(): base("/login")
    {
        Get["/"] = x => { return "Hello World!"; };
    }

}

After, I ran the project, all I could get was 404, Page Not Found error. So, one thing was sure. Nancy was up and running properly. However, any URL I enter was return a 404 page. The breakpoint was not get hit in the NancyLoginModule constructor. Which meant, the module was not being discovered by the Nancy framework.

The reason for this was me overlooking the sample code and missing the public keyword while declaring the NancyLoginModule class. We need to have the NancyModule class as public classes for the framework to find the classes and being able to handle the URL. Once the class was made public, I was able to see the "Hello World!" at "/login" URL.

The final module looked like:

public class NancyLoginModule : NancyModule
{
    public NancyLoginModule(): base("/login")
    {
        Get["/"] = x => { return "Hello World!"; };
    }

}
comments powered by Disqus