简体   繁体   中英

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.

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();
    }
}

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)

eg (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" 

Visual Studio构建事件

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM