Search code examples
c#nancy

How do i update live content on Nancy Hosting Framework


Iam using Nancy Framework with C# and my views are rendered from HTML files. If i need to udate the code in my HTML,CSS or JS (which is part of the content), I have to go through the long annoying process of

  1. Stopping the running C# project
  2. Editing my HTML,CSS or JS File
  3. Saving the changes
  4. Running the C# project
  5. Refreshing the browser
  6. Waiting for the connection to localhost:8080 to be established (This usually takes a bit longer on the first run and that is even more annoying) 7.Finally i get to see if that CSS styling has been applied...if it has not be applied, i have to start the whole process from 1.

Please help me if u have a better way of doing this if not maybe just explain to me why i always have to wait a few more seconds for the first run of the C# project to start allowing connections on the browser.


Solution

  • Nancy does not cache CSS or JS files, they are pass-through files when they exist under Content folder, or if you explicitly set them up in the static conventions.

    To disable View Caching, simply disable caches in your bootstrapper:

    StaticConfiguration.DisableCaches = true

    Edit:

    Based on your comment:

    https://github.com/richorama/Jukebox/blob/master/Jukebox/Controllers/Home.cs#L11

    namespace Jukebox.Controllers
    {
        public class Home : NancyModule
        {
            public Home()
            {
                Get["/"] = x =>
                {
                    return Response.AsFile("default.htm");
                };
            }    
        }
    }
    

    This will load the htm file off the disk every request. So I'm not entirely sure what issues you're running into. None-the-less try disabling the caches.

    Also caching is only enabled when you're running the code in Release mode...

    Edit 2:

    Since you're using a console app and files are copied to the bin.

    https://github.com/NancyFx/Nancy/wiki/The-root-path#changing-the-root-path

    Create a implementation of the rootpath provider pointing to your dev directory. Configure it to only run during development.