.net Owin Self主机没有缓存

时间:2016-05-04 18:20:50

标签: .net signalr owin

我有一个OWIN自托管应用程序,提供一个简单的开发Web服务器来提供单页HTML应用程序。由于我在外部编辑javascript,我想告诉服务器发回立即过期的缓存标头(以防止Chrome缓存)。

我需要在启动时添加什么(请注意,此服务器已启用文件浏览)。

   class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var hubConfiguration = new HubConfiguration();
            hubConfiguration.EnableDetailedErrors = (Tools.DebugLevel > 10);
            hubConfiguration.EnableJavaScriptProxies = true;
            app.UseCors(CorsOptions.AllowAll);
            //app.UseStaticFiles();
            app.UseFileServer(new FileServerOptions()
            {
                //RequestPath = new PathString("/Scopes"),
                EnableDirectoryBrowsing = true,
                FileSystem = new PhysicalFileSystem(@".\Scopes"),
            });
            app.MapSignalR("/signalr", hubConfiguration);
        }
    }

2 个答案:

答案 0 :(得分:0)

在microsoft asp论坛中获得了一些帮助:

https://forums.asp.net/p/2094446/6052100.aspx?p=True&t=635990766291814480

以下是我的工作并且效果很好:我在启动时添加了以下行:

app.Use(typeof(MiddleWare));

...

class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var hubConfiguration = new HubConfiguration();
        hubConfiguration.EnableDetailedErrors = (Tools.DebugLevel > 10);
        hubConfiguration.EnableJavaScriptProxies = true;
        app.UseCors(CorsOptions.AllowAll);
        //app.UseStaticFiles();
        app.Use(typeof(MiddleWare));
        app.UseFileServer(new FileServerOptions()
        {
            //RequestPath = new PathString("/Scopes"),
            EnableDirectoryBrowsing = true,
            FileSystem = new PhysicalFileSystem(@".\Scopes"),
        });
        app.MapSignalR("/signalr", hubConfiguration);
    }
}

然后我定义了我的中间件:

using Microsoft.Owin;
using System.Threading.Tasks;

namespace Gateway
{
    class MiddleWare : OwinMiddleware
    {
        public MiddleWare(OwinMiddleware next)
        : base(next)
        {
        }
        public override async Task Invoke(IOwinContext context)
        {
            context.Response.Headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
            context.Response.Headers["Pragma"] = "no-cache";
            context.Response.Headers["Expires"] = "0";
            await Next.Invoke(context);
        }
    }
}

答案 1 :(得分:0)

比新的中间件更简单的是像这样在StaticFileOptions中使用FileServerOptions

options.StaticFileOptions.OnPrepareResponse = context =>
{
     context.OwinContext.Response.Headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
     context.OwinContext.Response.Headers["Pragma"] = "no-cache";
     context.OwinContext.Response.Headers["Expires"] = "0";
};