11

在 ASP.NET 中,每当我从 VS2012 以调试模式运行服务器时,我对静态内容(js、css 等)所做的任何更改都会在保存后立即反映出来。

在 NancyFX 中,每次更改静态内容时都需要重新启动服务器。我假设这是因为每次运行服务器时 VS 都需要将静态内容复制到输出目录。

无论如何,保存后是否立即反映对静态内容所做的更改?

这是我对静态内容的配置

public class MainBootstrapper : DefaultNancyBootstrapper
{
    protected override void ConfigureConventions(NancyConventions nancyConventions)
    {
        nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("Scripts"));
        base.ConfigureConventions(nancyConventions);
    }
}

这可能也是相关的。我在一个控制台应用程序下运行它,nancyfx 主循环如下所示:

class Program
{
    const ushort port = 64402;
    const string escapeString = "/Terminate";

    static void Main(string[] args)
    {
        NancyHost host;

        #region Making new instance of NancyHost
        var uri = new Uri("http://localhost:" + port + "/");
        var config = new HostConfiguration(); config.UrlReservations.CreateAutomatically = true;

        host = new NancyHost(config, uri);
        #endregion
        #region NancyFX hosting loop
        try
        {
            host.Start();

            Console.Write("Start hosting the Fate/Another ranking system frontend\n" +
                "\t\"" + uri + "\"\n" +
                "To stop the hosting, input \"" + escapeString + "\".\n\n");
            do Console.Write("> "); while (Console.ReadLine() != escapeString) ;
        }
        catch (Exception e)
        {
            Console.WriteLine("Unhandled exception has been occured!\n"
                + e.Message);
            Console.ReadKey(true);
        }
        finally
        {
            host.Stop();
        }

        Console.WriteLine("Goodbye");
        #endregion
    }
}

如果您想知道为什么我不使用 Nancy.ASPNET.hosting,这将在带有 nginx 的 ubuntu 下运行

4

5 回答 5

7

Nancy 的默认根路径是bin您的应用程序的文件夹。如果您希望在刷新后反映资产的更新而不需要重建,您可以使用自定义Nancy.IRootPathProvider,您可以执行以下操作:

public class NancyCustomRootPathProvider : IRootPathProvider
{
    public string GetRootPath()
    {
#if DEBUG
        return Directory.GetParent(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)).FullName;
#else
        return Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
#endif
    }
}

这也允许生产构建直接从其bin目录提供服务,就像部署应用程序时的情况一样。

于 2015-05-19T15:01:33.110 回答
4

是不是因为您的站点是在 bin\ 目录之外执行的,并且您的静态内容是从该文件夹中提供的,并且在您编译时被复制到那里 - 所以当您在运行时更新静态内容时,它是您的源文件夹得到更新和不在您的 bin\ 文件夹中?

于 2013-09-20T09:49:50.803 回答
3

不确定您的确切设置是什么,但更新视图或静态内容并使其立即反映更改没有问题。我刚刚使用 Nancy.Hosting.Aspnet 主机在本地(使用 0.20.0)进行了尝试,效果很好

于 2013-09-15T18:21:18.083 回答
2

您可以按照https://github.com/NancyFx/Nancy/wiki/View-location-conventions中的说明将视图位置设置为解决方案源路径的位置。从 app.config 变量中获取位置,以便您可以根据运行的环境(调试/生产)进行切换。然后 nancy 将获取您保存在 IDE 中的视图。不要忘记设置StaticConfiguration.Caching.EnableRuntimeViewUpdates = true;,以便在页面刷新时获取所有更改。

于 2013-12-24T21:37:10.733 回答
2

您确定在服务器运行时保存更改文件吗?

IISExpress(对我来说,而不是对其他人)在运行时会锁定所有视图文件。这意味着我需要重新启动 IISExpress 才能保存任何更改。

也许类似的事情正在发生在你身上?

于 2013-09-16T08:59:27.800 回答