NancyFX 会立即反映静态内容的更改

本文关键字:静态 NancyFX | 更新日期: 2023-09-27 18:31:19

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

这将在 ubuntu w/nginx 下运行,以防您想知道为什么我不使用 Nancy.ASPNET.hosting

NancyFX 会立即反映静态内容的更改

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目录提供服务,就像部署应用程序时一样。

是因为您的网站正在 bin'' 目录之外执行,并且您的静态内容从该文件夹提供,并在您编译时复制到该文件夹 - 所以当您在运行时更新静态内容时,更新的是您的源文件夹而不是在您的 bin'' 文件夹中?

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

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

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

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

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