我有個問題。
我有一個簡單的服務,它執行請求并將回應流回檔案,它看起來像:
public async ValueTask GetWaetherByLocation(string place)
{
try
{
const string locationEndpoint = "https://localhost/api/place/?query=";
var fullEndpoint = $"{locationEndpoint}{place}";
HttpWebRequest request = WebRequest.CreateHttp(fullEndpoint);
WebResponse response = await request.GetResponseAsync();
ContentDispositionHeaderValue contentDisposition;
var fileName = ContentDispositionHeaderValue.TryParse(response.Headers["Content-Disposition"], out contentDisposition)
? contentDisposition.FileName
: "place.dat";
var filePath = Path.Combine(assemblyPathInfo, fileName);
using var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
await response.GetResponseStream().CopyToAsync(stream);
}
catch(Exception ex)
{
logger.Error(ex);
}
}
我的問題是我可以重用流來對某個模型進行回應,而無需再次發送請求或打開檔案資料被流式傳輸到嗎?
uj5u.com熱心網友回復:
為什么不使用回應快取中間件?
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCaching();
services.AddRazorPages();
}
按查詢鍵可以幫助您:
var responseCachingFeature = context.HttpContext.Features.Get<IResponseCachingFeature>();
if (responseCachingFeature != null)
{
responseCachingFeature.VaryByQueryKeys = new[] { "MyKey" };
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/345934.html
