c# - UseStaticFiles OnPrepareResponse 在运行时切换流
问题描述
是否可以用另一个图像流替换原始图像流?
看来我无法将 sr 分配给 HttpResponse
使用 asp 核心 2
有可能做到这一点还是有其他方法可以做到这一点?
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = x =>
{
if (x.Context.Request.Path.Value.StartsWith("/images/"))
{
using (var sr = File.OpenRead(path))
{
/// Here I need to replace the original stream with the stream from the sr.
x.Context.Response.Body = sr;
}
}
}
});
解决方案
这有效...
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = x =>
{
if (x.Context.Request.Path.Value.StartsWith("/images/"))
{
using (var sr = File.OpenRead(path))
{
HttpResponse hr = x.Context.Response;
hr.ContentLength = sr.Length;
sr.CopyTo(x.Context.Response.Body);
}
}
}
});
推荐阅读
- jestjs - 测试是否使用特定参数(来自组件)调用了 Vuex 操作
- regex - 用正则表达式匹配字符串结尾前的一个或多个 0 符号
- json - Apache Flink:无法从 ObjectNode::get 中提取密钥
- python - Django Rest 框架:自定义 IsReadOnly 权限
- elixir - 分配 @categories 在 eex 模板中不可用
- python - Windows 10 Toast 通知完成后退出我的 PyQt 应用程序
- python - 短文本 Python 的主题建模
- azure-application-insights - App Insights 记录我的 kusto 函数未显示在函数列表中
- nginx - 通过 nginx 代理请求不起作用,错误 404
- javascript - 表格提交在切换点击