(精华)2020年9月18日 ASP.NET Core 静态文件详解

默认静态文件的使用

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();
    }
}
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();
        }
    }
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();
        }
    }

设置默认页面

 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();
        }
    }
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 猿与汪的秘密 设计师:上身试试 返回首页