<img src="~/static/img1.jpg" />
我使用this article启用了静态文件,但是这些代码不起作用。
[Authorize]
public IActionResult BannerImage()
{
var file = Path.Combine(Directory.GetCurrentDirectory(),
"MyStaticFiles", "images", "banner1.svg");
return PhysicalFile(file, "image/svg+xml");
}
another article也无济于事。 但是我不知道如何仅将图像提供给授权用户?
更新
我添加了将WWWROOT配置为静态目录以设置缓存头的信息
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "WWWROOT"))
});
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "MyStaticFiles")),
OnPrepareResponse = x =>
{
if (x.Context.User.Identity.IsAuthenticated)
{
return;
}
x.Context.Response.StatusCode = (int) HttpStatusCode.Unauthorized;
}
});
然后授权停止工作。
答案 0 :(得分:3)
只需这样做
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = x =>
{
if (x.Context.User.Identity.IsAuthenticated)
{
return;
}
x.Context.Response.StatusCode = (int) HttpStatusCode.Unauthorized;
}
});
app.UseDefaultFiles();
}