[dotnetCore2.0]学习笔记之二: ASP.NET Core中,如何灵活使用静态文件和加载自定义配置

参考英文原文:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files

本文,只是学习笔记,记录部分知识点!!
 
对于NetCore2.0的变化,网上资料很多,比如razor page(有点类似于webform那会儿)、统一了标准、增加了一倍多的API、更加好的性能等等
 
从Program.cs文件中,也能看到很多变化,总体感觉,简约而不简单。
 public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }

功能点一:加载自定义配置文件

把一些加载appsetting.json配置的东西,都封装起来了。当然你也可以覆盖和扩展配置文件;

 public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((context, configBinder) =>
            {
                var env = context.HostingEnvironment;
                configBinder.AddJsonFile("otherSettings.json", optional: true, reloadOnChange: true)
                 .AddJsonFile($"otherSettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

            })
                .UseStartup<Startup>()
                .Build();

在项目根目录创建了otherSettings.json和otherSettings.Developement.json 两个文件;用于不同环境下,加载不同的配置测试;

# otherSettings.json文件中的配置内容

{
  "Environments": "Production"
}

比如,在About.cshtml.cs中进行测试,读取配置信息

public class AboutModel : PageModel
    {
        private IConfiguration Configuration { get; }
        public AboutModel(IConfiguration configuration)
        {
            this.Configuration = configuration;
        }

        public string Message { get; set; }

        public void OnGet()
        {
            Message = "Your application description page.[from ";
            Message += Configuration.GetSection("Environments").Value+"]";
        }
    }

输出内容如下:这样就读取到了相关自定一个配置文件;(本地配置的环境变量ASPNETCORE_ENVIRONMENT默认为Development)

Your application description page.[from Development]
Use this area to provide additional information.

功能点二:自定义静态文件文件夹和目录

比如你的文件目录结构如下:

wwwroot

  css

  js

  images

MyStaticFiles

  119.jpg

public void Configure(IApplicationBuilder app)
{
  app.UseStaticFiles(); // For the wwwroot folder
  app.UseStaticFiles(new StaticFileOptions()
  {
    FileProvider = new PhysicalFileProvider(
    Path.Combine(Directory.GetCurrentDirectory(), @"MyStaticFiles")),
    RequestPath = new PathString("/StaticFiles")
  });
}

dotnetcore默认是wwwroot文件夹下的资源文件(比如js,images,css等),如果你想创建一个自己的文件夹(比如名叫MyStaticFiles),可以通过对StaticFileOption进行设置;

通过以上设置,就可以通过访问http://{host:port}/StaticFiles/119.jpg,访问到119.jpg文件了; <img src="/StaticFiles/119.jpg"/>

设置cache-control,缓存10分钟;这样前端浏览器访问时,会在10min中返回304;

app.UseStaticFiles(new StaticFileOptions() {
                // 摘要:
                //     Called after the status code and headers have been set, but before the body has
                //     been written. This can be used to add or change the response headers.
                //public Action<StaticFileResponseContext> OnPrepareResponse { get; set; }

                OnPrepareResponse = ctx =>
                {
                    ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=600");
                }
            });

 tips:在我测试过程中,发现在VS中 只有利用 IISExpress 方式运行,才会生效;

功能点三:对指定文件进行鉴权

// 待完善

原文地址:https://www.cnblogs.com/hager/p/7419547.html