在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
如果使用Asp.net core来实现一个能够访问其它电脑上的资源如何将静态文件注入到项目中 app.UseStaticFiles();//提供将wwwroot目录开放访问,例如:http://localhost:52723/css/site.css将访问wwwroot目录下的css目录中的site.css文件
这方法的默认路径就是wwwroot目录
如何浏览目录的文件与文件夹 app.UseDirectoryBrowser(new DirectoryBrowserOptions()//提供文件目录访问形式 { FileProvider = new PhysicalFileProvider(@"D:\Study"), RequestPath = new PathString("/Study") });
app.UseFileServer(new FileServerOptions()//直接开启文件目录访问和文件访问 { EnableDirectoryBrowsing = true,//开启目录访问 FileProvider = new PhysicalFileProvider(@"D:\Git"), RequestPath = new PathString("/Git") });
/// <summary> /// 全盘符文件服务 /// </summary> /// <param name="app"></param> public static void UseLocalService(this IApplicationBuilder app) { DriveInfo[] driveInfos = DriveInfo.GetDrives(); foreach (DriveInfo drive in driveInfos) { string requestPath = drive.Name.Replace(":\\", ""); FileServerOptions fileServerOptions = new FileServerOptions { EnableDirectoryBrowsing = true, //RequestPath = $"/{requestPath}", FileProvider = new PhysicalFileProvider(drive.RootDirectory.FullName) }; fileServerOptions.StaticFileOptions.DefaultContentType = "application/x-msdownload";// fileServerOptions.StaticFileOptions.ServeUnknownFileTypes = true; FileExtensionContentTypeProvider exten = new FileExtensionContentTypeProvider(); exten.Mappings.Add(".log", "text/plain");//识别扩展类型 exten.Mappings.Add(".sln", "text/plain"); exten.Mappings.Add(".lng", "text/plain"); fileServerOptions.StaticFileOptions.ContentTypeProvider = exten; app.UseFileServer(fileServerOptions); } }
|
请发表评论