静态文件存储在项目的 Web 根目录中。 默认目录是 <content_root>/wwwroot,但可通过 UseWebRoot 方法更改目录。
public class Program { public static void Main(string[] args) { //初始化配置 GlobalConfig.Init(); new MicroInitializer() .Startup( new ServiceConfig { Assembly = typeof(TenanteServicePlugin).Assembly,IsRemote=true,ServerAddress= GlobalConfig.Settings["TenantUrl"] }, new ServiceConfig { Assembly = typeof(EInvoiceServicPlugin).Assembly, IsRemote = true, ServerAddress = GlobalConfig.Settings["InvoiceUrl"] } , new ServiceConfig { Assembly = typeof(CommonServiePlugin).Assembly } ); BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args).UseUrls("http://*:80") .UseStartup<Startup>() .Build(); }
WebHost.CreateDefaultBuilder(args)方法可将内容根目录设置为当前目录。
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); } //提供默认文档 app.UseDefaultFiles(); app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); //调用提供 wwwroot 文件夹中的静态文件 app.UseStaticFiles(new StaticFileOptions { //设置 HTTP 响应标头 OnPrepareResponse = ctx => { // Requires the following import: // using Microsoft.AspNetCore.Http; ctx.Context.Response.Headers.Append("key", "saas.server.web"); ctx.Context.Response.Headers.Append("token", "saas.inner"); }, FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "config")), RequestPath = "/json" }); //启用目录浏览 app.UseDirectoryBrowser(new DirectoryBrowserOptions { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "config")), RequestPath = "/json" }); } }
app.UseStaticFiles()方法重载将 Web 根目录中的文件标记为可用。
app.UseDefaultFiles():提供默认文档