kpk*_*pko 4 asp.net owin katana
当我使用 Microsoft.Owin.StaticFiles 时,如何在将响应发送到客户端之前修改响应?
FileServerOptions options = new FileServerOptions();
options.FileSystem = new Microsoft.Owin.FileSystems.PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, "Content/"));
options.DefaultFilesOptions.DefaultFileNames = new string[] { "index.htm", "index.html" };
options.StaticFileOptions.OnPrepareResponse = (r) =>
{
r.OwinContext.Response.WriteAsync("test");
};
options.EnableDefaultFiles = true;
app.UseFileServer(options);
Run Code Online (Sandbox Code Playgroud)
“测试”永远不会写入响应。我尝试使用另一个中间件,直到执行 StaticFiles 中间件:
app.Use((ctx, next) =>
{
return next().ContinueWith(task =>
{
return ctx.Response.WriteAsync("Hello World!");
});
});
FileServerOptions options = new FileServerOptions();
options.FileSystem = new Microsoft.Owin.FileSystems.PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, "Content/"));
options.DefaultFilesOptions.DefaultFileNames = new string[] { "index.htm", "index.html" };
options.EnableDefaultFiles = true;
app.UseFileServer(options);
Run Code Online (Sandbox Code Playgroud)
但这没有用。如何修改响应?
准备响应并不意味着修改静态文件的内容。您只能添加标题。
我需要将一些更改的变量传递给静态网页,我通过使用 On prepare 响应绕过它,并将变量作为页面的 cookie 传递。这适用于一些变量,但如果您想显着更改页面,最好使用 mvc 组件。
appBuilder.UseStaticFiles(new StaticFileOptions()
{
RequestPath = new PathString(baseUrl),
FileSystem = new PhysicalFileSystem(staticFilesLocation),
ContentTypeProvider = new JsonContentTypeProvider(),
OnPrepareResponse = r => r.OwinContext.Response.Cookies.Append("baseUrl",_webhostUrl)
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1485 次 |
| 最近记录: |