2

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.

Paul
  • 207
  • 1
  • 3
  • 14
  • No spender you're wrong. – Phill Jul 07 '14 at 14:18
  • @Phill Yes I am! A postbuild event could do an xcopy or similar... see here: http://stackoverflow.com/questions/10827024/copying-visual-studio-project-files-to-output-directory-during-build – spender Jul 07 '14 at 14:50

3 Answers3

2

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();
    }
}
Phill
  • 18,398
  • 7
  • 62
  • 102
  • I believe that you've misunderstood what OP is asking. They have made a (non-web) project where the executable is created by VS in the `/bin` folder and run. This means that (unlike in a web project) any content files must also be copied into the `/bin` folder by explicitly changing the properties for each one of these files to "copy to output directory". OP is asking if this property change can be automated in some way. – spender Jul 07 '14 at 14:39
  • OH, I stand corrected. In that case I just copy as part of a build event in the project file or a build task on the CI. – Phill Jul 07 '14 at 14:49
  • Thanks for the info - so if I wanted to use a post build event what is the switch to preserve the file structure /t retains the file structure but does not copy files I want to retain directory structure and copy files? – Paul Jul 07 '14 at 16:02
2

I had the same issue and I found a workaround that others might find useful:

Instead of copying the files to the output directory on each build, I created a directory junction in it, targeting the original static-files directories.

This allows real-time editing of the static content in Visual-Studio (without the need to rebuild in order to copy the edited files to the output directory)

e.g. (Post-build command line):

if not exist "$(TargetDir)Web" md "$(TargetDir)Web"
if not exist "$(TargetDir)Web\Content" mklink /j "$(TargetDir)Web\Content" "$(ProjectDir)Content" 
if not exist "$(TargetDir)Web\Scripts" mklink /j "$(TargetDir)Web\Scripts" "$(ProjectDir)Scripts" 
if not exist "$(TargetDir)Web\Fonts" mklink /j "$(TargetDir)Web\Fonts" "$(ProjectDir)Fonts" 
if not exist "$(TargetDir)Web\Static" mklink /j "$(TargetDir)Web\Static" "$(ProjectDir)Web\Static" 
Eitan H.S.
  • 430
  • 3
  • 15
0

Visual studio build events

You can use Visual studio build events and add xcopy command like this:

xcopy /E /Y "$(ProjectDir)\Views" "$(ProjectDir)\bin\$(ConfigurationName)\Views\*"
xcopy /E /Y "$(ProjectDir)\Content" "$(ProjectDir)\bin\$(ConfigurationName)\Content\*"

when project is built xcopy gets executed and files are copied in output dir, so your selfhost exe can see that files.

Davit Tvildiani
  • 1,915
  • 3
  • 19
  • 29