【.Net Core】处理静态文件

静态文件存储在项目的 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():提供默认文档

转载于:https://www.cnblogs.com/xuwendong/p/8901840.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值