zoukankan      html  css  js  c++  java
  • 【.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():提供默认文档

  • 相关阅读:
    【Kubernetes学习笔记】-kubeadm 手动搭建kubernetes 集群
    教你快速搭建NFS服务
    【Kubernetes学习笔记】-服务访问之 IP & Port & Endpoint 辨析
    【Kubernetes学习笔记】-使用Minikube快速部署K8S单机学习环境
    Linux RDP 会话中无法打开VSCode 解决办法
    Jenkins 凭证管理
    linux 后台运行进程:& , nohup
    使用私有gitlab搭建gitbook持续集成
    VS Code 使用
    Markdown Rules 详解
  • 原文地址:https://www.cnblogs.com/xuwendong/p/8901840.html
Copyright © 2011-2022 走看看