南希无法在自定义约定中找到静态内容 [英] Nancy fails to find static content in custom convention

查看:78
本文介绍了南希无法在自定义约定中找到静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个Nancy引导程序,以从非默认目录路径(它是自托管的Nancy)中提供静态内容.

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

奇怪的是,以下内容适用于自定义View位置约定,但不适用于js或CSS静态内容约定(是的,文件和文件夹都位于这些位置!).我尝试解决此问题的尝试更加复杂,因为我还没有弄清楚如何记录在找不到静态内容时发生的错误.

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

Chrome和ProcMon的输出如下:

The output from Chrome and ProcMon is as follows:

我应该如何:

  1. 记录未找到的js和css文件时发生的错误吗?
  2. 使用静态文件约定解决404错误?

推荐答案

可以代替使用

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.

这篇关于南希无法在自定义约定中找到静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆