欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# .Net Core静态文件服务器的新人入门教程

程序员文章站 2022-06-24 23:13:20
概要: 本文通过示例,讲解了 net core2.0 静态文件目录的相关知识,并附带解析,适合新手,并附带了完整的项目代码。(项目通过 vs2017 初始化的 asp.n...

概要:

本文通过示例,讲解了 net core2.0 静态文件目录的相关知识,并附带解析,适合新手,并附带了完整的项目代码。(项目通过 vs2017 初始化的 asp.net core 应用程序,之后选择***空项目***)

示例代码

项目结构

c# .Net Core静态文件服务器的新人入门教程

program.cs文件

using system;
using system.collections.generic;
using system.io;
using system.linq;
using system.threading.tasks;
using microsoft.aspnetcore;
using microsoft.aspnetcore.hosting;
using microsoft.extensions.configuration;
using microsoft.extensions.logging;

namespace staticfileserver
{
 public class program
 {
 public static void main(string[] args)
 {
  buildwebhost(args).run();
 }

 public static iwebhost buildwebhost(string[] args) =>
  webhost.createdefaultbuilder(args)
  .usekestrel()
  .usecontentroot(directory.getcurrentdirectory()) // 设置当前目录的内容
  .useiisintegration()
  .useurls("http://*:5000") // 使 项目在 5000端口被访问
  .usestartup<startup>()
  .build();
 }
}

startup.cs 文件

using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;
using microsoft.aspnetcore.builder;
using microsoft.aspnetcore.hosting;
using microsoft.aspnetcore.http;
using microsoft.aspnetcore.staticfiles;
using microsoft.extensions.dependencyinjection;
using microsoft.extensions.fileproviders;

namespace staticfileserver
{
 public class startup
 {
 // this method gets called by the runtime. use this method to add services to the container.
 // for more information on how to configure your application, visit https://go.microsoft.com/fwlink/?linkid=398940
 public void configureservices(iservicecollection services)
 {
  
 }

 // this method gets called by the runtime. use this method to configure the http request pipeline.
 public void configure(iapplicationbuilder app, ihostingenvironment env)
 {
  app.usestaticfiles(); // 使用默认文件夹 wwwroot 仅仅shi wwwroot对外可见
  
  app.run(async (context) => 
  {
  await context.response.writeasync("hello jesus");
  });
 }
 }
}

运行效果:

c# .Net Core静态文件服务器的新人入门教程

解析: 这是一个基本的静态文件服务器,app.usestaticfiles() 函数使当前内容目录下默认的 wwwroot中的文件可以被访问

那么问题来了,若想访问其他目录下的静态文件,该怎么办?

设置任意目录下的静态文件可以访问代码:

 // 设置 指定目录的文件 可以被访问 start
  var staticfile = new staticfileoptions();
  staticfile.fileprovider = new physicalfileprovider(@"c:\"); // 指定目录,这里指的是c盘,也可以指定其他目录
  app.usestaticfiles(staticfile); 

我们吧startup.cs的***configure*** 函数代码改为如下代码(增加了c盘文件可以访问):

 // this method gets called by the runtime. use this method to configure the http request pipeline.
 public void configure(iapplicationbuilder app, ihostingenvironment env)
 {
  var staticfile = new staticfileoptions();
  staticfile.fileprovider = new physicalfileprovider(@"c:\"); // 指定目录,这里指的是c盘,也可以指定其他目录
  app.usestaticfiles(staticfile); // 使用默认文件夹 wwwroot 仅仅shi wwwroot对外可见
  
  app.run(async (context) => 
  {
  await context.response.writeasync("hello jesus");
  });
 }

c盘文件展示

c# .Net Core静态文件服务器的新人入门教程

运行效果

c# .Net Core静态文件服务器的新人入门教程

这样我们就可以访问任意目录下的文件了,那么问题来了,c盘中有个 叫 456.log 的文件,我们访问不了,原因是:服务器不能识别,怎么办?如何让服务器识别 所有类型的文件呢?  我们以 .log 为后缀的文件为例

我们将***configure*** 改为一下内容:

// this method gets called by the runtime. use this method to configure the http request pipeline.
 public void configure(iapplicationbuilder app, ihostingenvironment env)
 {
  var staticfile = new staticfileoptions();
  staticfile.fileprovider = new physicalfileprovider(@"c:\"); // 指定目录,这里指的是c盘,也可以指定其他目录

  // 设置 对应的文件类型(防止mime type没事别出来,打不开或出现404错误)
  staticfile.serveunknownfiletypes = true;
  staticfile.defaultcontenttype = "application/x-msdownload";// 设置默认 mime type
  var provider = new fileextensioncontenttypeprovider();
  provider.mappings.add(".log", "text/plain"); // 手动设置对应的 mime type
  staticfile.contenttypeprovider = provider;

  app.usestaticfiles(staticfile); // 使用默认文件夹 wwwroot 仅仅shi wwwroot对外可见
  // 设置 指定目录的文件 可以被访问 end

  app.run(async (context) =>
  {
  await context.response.writeasync("hello jesus");
  });
 }

我们将不能识别的文件类型默认为 : "application/x-msdownload",即遇到我们没处理的,不能识别的类型统统下载下来。

provider.mappings.add(".log", "text/plain"); // 手动设置对应的 mime type 。我们手动增加了 对后缀为.log的文件类型的处理,当成文本文件处理,即txt处理。

运行效果

未知的文件 (我们访问789.ggg文件,此文件类型我们未处理过)

c# .Net Core静态文件服务器的新人入门教程

已处理的文件类型

c# .Net Core静态文件服务器的新人入门教程

这样,我们就可以访问任意类型的静态文件了,那么问题又来了, 我想访问一个目录下所有的文件,即访问某个目录怎么办?

在 net core 中访问目录的功能默认是禁止的,需要手动开启。

步骤:

1、在 configureservices 函数中增加 目录访问服务,

 public void configureservices(iservicecollection services)
  {
   services.adddirectorybrowser(); // 使目录可以被浏览 (浏览所有的文件以及文件夹)
  }

2、在configure 函数中增加 中间键 和 具体的目录,在这里我们让 c盘下的所有目录可以被访问

 // 设置 目录可浏览 start
   var dir = new directorybrowseroptions();
   dir.fileprovider = new physicalfileprovider(@"c:\");
   app.usedirectorybrowser(dir);
   // 设置 目录可浏览 end

这样我们就可以访问c盘中的任意目录了,效果如下:

c# .Net Core静态文件服务器的新人入门教程

startup.cs 文件最终代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;
using microsoft.aspnetcore.builder;
using microsoft.aspnetcore.hosting;
using microsoft.aspnetcore.http;
using microsoft.aspnetcore.staticfiles;
using microsoft.extensions.dependencyinjection;
using microsoft.extensions.fileproviders;

namespace staticfileserver
{
 public class startup
 {
  // this method gets called by the runtime. use this method to add services to the container.
  // for more information on how to configure your application, visit https://go.microsoft.com/fwlink/?linkid=398940
  public void configureservices(iservicecollection services)
  {
   services.adddirectorybrowser(); // 使目录可以被浏览 (浏览所有的文件以及文件夹)
  }

  // this method gets called by the runtime. use this method to configure the http request pipeline.
  public void configure(iapplicationbuilder app, ihostingenvironment env)
  {
   // 设置 目录可浏览 start
   var dir = new directorybrowseroptions();
   dir.fileprovider = new physicalfileprovider(@"c:\");
   app.usedirectorybrowser(dir);
   // 设置 目录可浏览 end

   // 设置 指定目录的文件 可以被访问 start
   var staticfile = new staticfileoptions();
   staticfile.fileprovider = new physicalfileprovider(@"c:\"); // 指定目录,这里指的是c盘,也可以指定其他目录

    // 设置 对应的文件类型(防止mime type没事别出来,打不开或出现404错误)
    staticfile.serveunknownfiletypes = true;
    staticfile.defaultcontenttype = "application/x-msdownload";// 设置默认 mime type
    var provider = new fileextensioncontenttypeprovider();
    provider.mappings.add(".log", "text/plain"); // 手动设置对应的 mime type
    staticfile.contenttypeprovider = provider;

   app.usestaticfiles(staticfile); // 使用默认文件夹 wwwroot 仅仅shi wwwroot对外可见
   // 设置 指定目录的文件 可以被访问 end

   app.run(async (context) => 
   {
    await context.response.writeasync("hello jesus");
   });
  }
 }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。