加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

浅谈ASP.NET Core静态文件处理源码探究

发布时间:2020-08-21 13:45:16 所属栏目:Asp教程 来源:网络整理
导读:副标题#e# 静态文件(如 HTML、CSS、图像和 JavaScript)等是Web程序的重要组成部分。传统的ASP.NET项目一般都是部署在IIS上,IIS是一个功能非常强大的服务器平台,可以直接处理接收到的静态文件处理而不需要经过应用程序池处理,所以很多情况下对于静态文

public class StaticFileMiddleware { private readonly StaticFileOptions _options; private readonly PathString _matchUrl; private readonly RequestDelegate _next; private readonly ILogger _logger; private readonly IFileProvider _fileProvider; private readonly IContentTypeProvider _contentTypeProvider; public StaticFileMiddleware(RequestDelegate next, IWebHostEnvironment hostingEnv, IOptions<StaticFileOptions> options, ILoggerFactory loggerFactory) { _next = next; _options = options.Value; //设置文件类型提供程序 _contentTypeProvider = options.Value.ContentTypeProvider ?? new FileExtensionContentTypeProvider(); //文件提供程序 _fileProvider = _options.FileProvider ?? Helpers.ResolveFileProvider(hostingEnv); //匹配路径 _matchUrl = _options.RequestPath; _logger = loggerFactory.CreateLogger<StaticFileMiddleware>(); } public Task Invoke(HttpContext context) { //判断是够获取到终结点信息,这也就是为什么我们使用UseStaticFiles要在UseRouting之前 if (!ValidateNoEndpoint(context)) { } //判断HttpMethod,只能是Get和Head操作 else if (!ValidateMethod(context)) { } //判断请求路径是否存在 else if (!ValidatePath(context, _matchUrl, out var subPath)) { } //根据请求文件名称判断是否可以匹配到对应的MimeType,如果匹配到则返回contentType else if (!LookupContentType(_contentTypeProvider, _options, subPath, out var contentType)) { } else { //执行静态文件操作 return TryServeStaticFile(context, contentType, subPath); } return _next(context); } private Task TryServeStaticFile(HttpContext context, string contentType, PathString subPath) { var fileContext = new StaticFileContext(context, _options, _logger, _fileProvider, contentType, subPath); //判断文件是否存在 if (!fileContext.LookupFileInfo()) { _logger.FileNotFound(fileContext.SubPath); } else { //静态文件处理 return fileContext.ServeStaticFile(context, _next); } return _next(context); } }

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读