(精华)2020年9月18日 ASP.NET Core 静态文件详解
【摘要】
默认静态文件的使用
public static class Sample03
{
public static void Run()
{
var path = Path.C...
默认静态文件的使用
public static class Sample03
{
public static void Run()
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "content");
var fileProvider = new PhysicalFileProvider(path);
var staticFileOptions = new StaticFileOptions
{
FileProvider = fileProvider,
RequestPath = "/content"
};
// 目录浏览中间件配置
var directoryBrowserOptions = new DirectoryBrowserOptions
{
FileProvider = fileProvider,
RequestPath = "/content"
};
Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder.Configure(
app=>app
// 目录浏览中间件
.UseStaticFiles()
.UseStaticFiles(staticFileOptions)
.UseDirectoryBrowser()
.UseDirectoryBrowser(directoryBrowserOptions)))
.Build()
.Run();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
public static class Sample05
{
public static void Run()
{
var options = new StaticFileOptions
{
ServeUnknownFileTypes = true,
DefaultContentType = "image/jpg"
};
Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder.Configure(
app=>app
.UseStaticFiles(options)
.UseDirectoryBrowser()))
.Build()
.Run();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
public static class Sample06
{
public static void Run()
{
var contentTypeProvider = new FileExtensionContentTypeProvider();
contentTypeProvider.Mappings.Add(".abc", "images/jpg");
var options = new StaticFileOptions
{
ContentTypeProvider = contentTypeProvider
};
Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder.Configure(
app=>app
.UseStaticFiles(options)
.UseDirectoryBrowser()))
.Build()
.Run();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
设置默认页面
public class Sample04
{
public static void Run()
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "content");
var fileProvider = new PhysicalFileProvider(path);
var staticFileOptions = new StaticFileOptions
{
FileProvider = fileProvider,
RequestPath = "/content"
};
var directoryBrowserOptions = new DirectoryBrowserOptions
{
FileProvider = fileProvider,
RequestPath = "/content"
};
// 默认页面中间件配置
var defaultFilesOptions = new DefaultFilesOptions
{
FileProvider = fileProvider,
RequestPath = "/content"
};
defaultFilesOptions.DefaultFileNames.Add("readme.html");
Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder.Configure(
app=>app
// 默认页面中间件
.UseDefaultFiles()
.UseDefaultFiles(defaultFilesOptions)
.UseStaticFiles()
.UseStaticFiles(staticFileOptions)
.UseDirectoryBrowser()
.UseDirectoryBrowser(directoryBrowserOptions)))
.Build()
.Run();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/108524661
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)