• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >ASP.NET > c# .Net Core静态文件服务器的新人入门教程

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

作者:MrITzhongzi 字体:[增加 减小] 来源:互联网 时间:2018-08-20

MrITzhongzi 通过本文主要向大家介绍了.net,core,文件服务器,.net,core,静态文件,静态文件服务器等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

概要:

本文通过示例,讲解了 NET Core2.0 静态文件目录的相关知识,并附带解析,适合新手,并附带了完整的项目代码。(项目通过 vs2017 初始化的 ASP.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");
  });
 }
 }
}

运行效果:

解析: 这是一个基本的静态文件服务器,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盘中有个 叫 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文件,此文件类型我们未处理过)

已处理的文件类型

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

在 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盘中的任意目录了,效果如下:

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");
   });
  }
 }
}

总结

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

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Asp.net SignalR 应用并实现群聊功能 开源代码
  • asp.net动态更新
  • asp.net利用母版制作页脚效果
  • Asp.Net服务器发送HTTP标头后无法设置内容类型的问题解决
  • 使用asp.net mvc,boostrap及knockout.js开发微信自定义菜单编辑工具(推荐)
  • 详解ASP.NET MVC 常用扩展点:过滤器、模型绑定
  • ASP.NET Core发送邮件的方法
  • 在ASP.NET Core 中发送邮件的实现方法(必看篇)
  • ASP.NET MVC从视图传参到控制器的几种形式
  • .net core使用redis基于StackExchange.Redis

相关文章

  • 2017-05-11写给初学asp.net的新人们 新手学习经验
  • 2017-05-11ASP.NET MVC 微信JS-SDK认证
  • 2017-05-11asp.net TextBox控件设置ReadOnly后,不能回传。
  • 2018-08-20.Net Core和jexus配置HTTPS服务方法
  • 2017-05-11在Global.asax文件里实现通用防SQL注入漏洞程序(适应于post/get请求)
  • 2017-05-11asp.net利用反射实现给model类赋值的方法
  • 2017-05-11在ASP.NET 2.0中操作数据之二十八:GridView里的Button
  • 2017-05-11通过HttpClient 调用ASP.NET Web API示例
  • 2017-05-11Global.asax取绝对路径的方法
  • 2017-05-11ASP.NET MVC自定义错误页面真的简单吗?

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Log4net日志记录组件的使用步骤详解和下载
    • asp.net中简体转繁体实现代码
    • LINQ to XML的编程基础
    • ASP.NET MVC学习教程之Razor语法
    • Asp.Net数据控件引用AspNetPager.dll分页实现代码
    • 测试控制台使用方法
    • Forms身份认证在IE11下无法保存Cookie的问题
    • ASP.NET MVC自定义错误页面真的简单吗?
    • asp.net网络数据库开发实例精解 源文件
    • WPF实现ScrollViewer滚动到指定控件处

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有