Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nancy fails to find static content in custom convention

Tags:

nancy

I've set up a Nancy bootstrapper to serve static content from a non-default directory path (it's self hosted Nancy).

Strangely, the following works for the custom View location convention but not either of the js or css static content conventions (and yes, both files and folders exist at these locations!). My attempts at trying to resolve this are further compounded as I haven't figured out how to log errors which occur when static content is not found.

using System;
using System.IO;

using Nancy;
using Nancy.Conventions;
using Nancy.Bootstrapper;
using Nancy.TinyIoc;

namespace MyApp
{
    public class ApplicationBootstrapper : DefaultNancyBootstrapper
    {

    private const string RELATIVE_PATH_TO_SOURCE = @"../static/MyApp/";

    protected override void ConfigureConventions(NancyConventions nancyConventions)
    {

        nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("js", string.Concat(RELATIVE_PATH_TO_SOURCE, "Scripts/")));
        nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("css", string.Concat(RELATIVE_PATH_TO_SOURCE, "Content/")));
        this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
        {
            return string.Concat(RELATIVE_PATH_TO_SOURCE, "Views/", viewName);
        });
        this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
        {
            return string.Concat(RELATIVE_PATH_TO_SOURCE, "Views/", context.ModuleName, "/", viewName);
        });

        base.ConfigureConventions(nancyConventions);
    }

    protected override IRootPathProvider RootPathProvider
    {
        get
        {
            return new MyRootPathProvider();
        }
    }

    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
    {
        pipelines.OnError += (ctx, ex) =>
        {
            Console.WriteLine("RootPath : {0}", DebugRootPathProvider.RootPath);
            Console.WriteLine("Unhandled error on request: {0} : {1}", ctx.Request.Url, ex.Message); //HACK
            Console.WriteLine(ex.StackTrace); //HACK poor man's logging
            return null;
        };
    }
}

public class MyRootPathProvider : IRootPathProvider
{
    public static readonly string RootPath;
    static MyRootPathProvider()
    {
        RootPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
    }

    public string GetRootPath()
    {
        return RootPath;
    }
}
}

The output from Chrome and ProcMon is as follows: ProcMon outputGoogle Chrome output

How should I:

  1. Log errors occurring with not found js and css files?
  2. Resolve the 404 errors with the static file conventions?
like image 703
Iain Sproat Avatar asked Jan 14 '14 20:01

Iain Sproat


2 Answers

Instead of logging you can use sysinternals process monitor and look for what files the nancy process (exe or IIS worker process) are attempting to read.

like image 129
Justin Dearing Avatar answered Nov 09 '22 19:11

Justin Dearing


I had problems with serving static files (in my case js files) in a self host environment as well. They were not recognized at all, not even in the default "Content" folder. My solution: I installed the Microsoft StaticFiles NuGet Package.

In the startup class, register a static files folder like this:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseStaticFiles("/Scripts");
        app.MapSignalR();
        app.UseNancy();
    }
}

This way, all files and subfolders in the "Scripts" folder are served as static files. There is also an overload of the UseStaticFiles() function that lets you map a physical path to a virtual path.

Important here: the call to UseNancy() has to be the very last, otherwise everything after it won't work. I also tried to combine it with SignalR, as you can see above, and there the UseNancy() call had to be at the end as well.

like image 27
Jan Mattner Avatar answered Nov 09 '22 18:11

Jan Mattner