Question

I love what Nancy has to offer, especially in it's capability to promote module reuse.

I am trying to run a nancy module off an existing ASP.NET Webforms application. I have followed every variation of the official documentation (https://github.com/NancyFx/Nancy/wiki/Hosting-Nancy-with-asp.net) but I cannot get it to work at all.

To illustrate this I created a blank web forms project in VS2013, using one of the available templates, and then I tried to drop in a minimal nancy configuration.

You can download this solution from here: https://www.dropbox.com/s/icq78zfgi92a6xv/HelloWorldNancy.zip

As per the documentation above I have re-rooting my nancy application in the http handlers to the 'nancy/*' subpath.

I added a module called HelloWorldModule, like so:

public class HelloWorldModule : NancyModule
{
    public HelloWorldModule()
        : base("/helloworld")
    {
        Get["/"] = _ =>
        {
            return View["index"];
        };
    }
}

I also added a basic index.sshtml view to /Views/HelloWorld/ and /Nancy/Views/HelloWorld just in case it decided to do the View resolution against the /Nancy root.

I can run my project and the existing ASP.NET application is unaffected.

When I hit the '/nancy' subpath I notice that the HelloWorldModule constructor is hit. I get back the fancy nancy 404 page.

Then when I try to hit '/nancy/helloworld' I get nothing, simply a 404 again. I have my debugger running and I get nothing back.

I also tried to run the nancy diagnostics tool '/nancy/_Nancy' but that didn't work either.

Does anyone out there have a clue as to what I am doing wrong here?


UPDATE [2014/05/02]

I have created a screencast illustrating the steps I took to do this, using a base Web Application template from Visual Studio: http://www.screencast.com/t/S8yEXPC74y

The screencast ran out of time just before I could execute the alternative configuration proposed by the official documentation. When I ran with this configuration I simply got blank pages for every request (500 response codes).

Was it helpful?

Solution

Oki, I have figured out how to get it working, although I am not entirely sure this is correct/expected behaviour.

Although I was trying to integrate into an ASP.NET WebForms Web Application I decided to read the documentation for integrating into an MVC application. In this part of the documentation, it currently (2014/05/02) states that you need to include the custom base path within the root path of your modules.

So in my case my module definition changes from:

public class HelloWorldModule : NancyModule
{
    public HelloWorldModule()
        : base("/helloworld")
    {
        Get["/"] = _ => "Hello world!";
    }
}

To:

public class HelloWorldModule : NancyModule
{
    public HelloWorldModule()
        : base("/nancy/helloworld")
    {
        Get["/"] = _ => "Hello world!";
    }
}

Now when I hit the url '/nancy/helloworld' I get back the expected response. This is not ideal as my compiled modules will need to know about a dynamic configuration variable. As I am building a set of reusable Nancy modules for our codebase this is a bit of a problem for me, but it is one that I can look into solving now that I at least know how my solution needs to be configured.

I have created an example working project for download: https://www.dropbox.com/s/v895gk3slnz0ggv/Bob.zip

OTHER TIPS

Well first up, you've made a Web Site, not a Web Application. Putting all your code in a Web App works, I can't get it to work in a Web Site. (with some other changes)

Web Sites should be removed from Visual Studio, they are the worst thing ever added.

Second is I removed the web.config entries and uncommented the code you had commented out, and fixed the path to be * instead of nancy/*

tl/dr?

  1. Don't use a Web Site, use a Web App.
  2. Fix the location
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top