Search code examples
c#console-applicationnancyself-hosting

Nancy Self Host and Static Content Files


Am I right in thinking that if you create a self host nancy console app and want to serve up html,javascript and css files that you have to go thru all these files (could be quite a few) and mark them all for copy to output directory.

public class HomeModule : NancyModule
{
    public HomeModule()
    {

        Get["/"] = v =>  View["index.html"];
    }
}

This will not be found if the index.html file is in the project folder and is not marked copy to output on it's properties.


Solution

  • Edit: I stand corrected, I misunderstood the question.

    Yes you need to set all static content to copy, however when I setup my project's (I can't copy paste an example for you at the moment), I just add a Build Event in the project file, or I setup a Build Task for the CI / deployment.


    Nope, you don't need to mark every file individually.

    https://github.com/NancyFx/Nancy/wiki/Managing-static-content

    You can mark an entire directory.

    Alternatively, if you're using OWIN, you can use the Static Content middleware.

    Something like:

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var fileSystem = new FileServerOptions
            {
                EnableDirectoryBrowsing = false,
                FileSystem = new PhysicalFileSystem("....")
            };
    
            app.UseFileServer(fileSystem);
            app.UseNancy();
        }
    }