{"id":211395,"date":"2021-02-21T09:14:13","date_gmt":"2021-02-21T01:14:13","guid":{"rendered":"https:\/\/gulass.cn\/?p=211395"},"modified":"2021-02-10T09:15:05","modified_gmt":"2021-02-10T01:15:05","slug":"asp-net-core-uses-file-providers","status":"publish","type":"post","link":"https:\/\/gulass.cn\/asp-net-core-uses-file-providers.html","title":{"rendered":".Net Core\u4f7f\u7528File Providers"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nASP.Net Core \u4e3a\u4e86\u4fbf\u4e8e\u83b7\u53d6\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u4fe1\u606f\uff0c\u76d1\u89c6\u6587\u4ef6\u53d8\u66f4\uff0c \u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2a\u62bd\u8c61\u5c42\uff1aFile Providers\uff0c \u8fd9\u7bc7\u6587\u7ae0\u5c06\u4f1a\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528 File Providers \u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
File Provider \u62bd\u8c61\u5c42<\/strong><\/div>\n

file prodivers \u5b9e\u73b0\u4e86 IFileProvider \u63a5\u53e3\uff0c\u5b83\u7684\u5b9e\u73b0\u5b50\u7c7b\u5927\u6982\u6709\u4e09\u5757: Physical,Embedded \u548c Composite \uff0c\u540c\u65f6\u8fd9\u4e2a\u63a5\u53e3\u63d0\u4f9b\u4e86\u53ef\u7528\u4e8e\u83b7\u53d6\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u7684\u76f8\u5173\u4fe1\u606f\u7684\u65b9\u6cd5\uff0c\u63a5\u53e3\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n

\r\npublic interface IFileProvider \r\n    { \r\n        IDirectoryContents GetDirectoryContents(string subpath); \r\n \r\n        IFileInfo GetFileInfo(string subpath); \r\n \r\n        IChangeToken Watch(string filter); \r\n    }\r\n<\/pre>\n

\u5982\u679c\u8981\u5b9e\u73b0 \u66f4\u6539\u901a\u77e5\uff0c\u8fd8\u9700\u8981\u7528 IChangeToken \u63a5\u53e3\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\npublic interface IChangeToken \r\n   { \r\n       bool ActiveChangeCallbacks { get; } \r\n \r\n       bool HasChanged { get; } \r\n \r\n       IDisposable RegisterChangeCallback(Action callback, object state); \r\n   } \r\n<\/object><\/pre>\n

\u4e0b\u9762\u662f IFileProvider \u63a5\u53e3\u7684\u4e09\u4e2a\u5177\u4f53\u5b9e\u73b0\u53ca\u5e94\u7528,\u7f57\u5217\u5982\u4e0b\uff1a<\/p>\n

    PhysicalFileProvider<\/ol>\n

    \u7528\u4e8e\u5bf9 \u7269\u7406\u6587\u4ef6 \u7684\u8bbf\u95ee<\/p>\n

      EmbeddedFileProvider<\/ol>\n

      \u7528\u4e8e\u5bf9 \u7a0b\u5e8f\u96c6 \u5185\u7684\u6587\u4ef6\u8bbf\u95ee<\/p>\n

        CompositeFileProvider<\/ol>\n

        \u5b9e\u73b0\u524d\u9762\u4e24\u8005\u7684\u6df7\u5408\u8bbf\u95ee<\/p>\n

        \u4f7f\u7528 PhysicalFileProvider<\/p>\n

        \u8981\u60f3\u5728 ASP.Net Core \u4e2d\u6f14\u793a PhysicalFileProvider\uff0c\u9996\u5148\u5c06\u5176\u6ce8\u5165\u5230 ServiceCollection \u5bb9\u5668\u4e2d\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n

        \r\npublic void ConfigureServices(IServiceCollection services) \r\n        { \r\n            services.AddControllersWithViews(); \r\n \r\n            IFileProvider physicalProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()); \r\n \r\n            services.AddSingleton(physicalProvider); \r\n        } \r\n<\/ifileprovider><\/pre>\n

        \u6ce8\u5165\u5230\u5bb9\u5668\u540e\uff0c\u73b0\u5728\u5c31\u53ef\u4ee5\u5728 Controller \u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\u83b7\u53d6\u5b9e\u4f8b\u4e86\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n

        \r\npublic class HomeController : Controller \r\n   { \r\n       private readonly ILogger _logger; \r\n       private readonly IFileProvider _fileProvider; \r\n \r\n       public HomeController(ILogger<\/homecontroller> logger, IFileProvider fileProvider) \r\n       { \r\n           _logger = logger; \r\n           this._fileProvider = fileProvider; \r\n       } \r\n   } \r\n<\/homecontroller><\/pre>\n

        \u4e0b\u9762\u5728 Index \u65b9\u6cd5\u4e2d\u4f7f\u7528 GetDirectoryContents() \u6765\u83b7\u53d6\u5f53\u524d\u9879\u76ee\u6587\u4ef6\u5939\u4e0b\u7684\u6240\u6709\u6587\u4ef6\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n

        \r\npublic IActionResult Index() \r\n       { \r\n           List lst = new List<\/string>(); \r\n           var contents = _fileProvider.GetDirectoryContents(\"\"); \r\n \r\n           foreach (IFileInfo fileInfo in contents) \r\n           { \r\n               lst.Add(fileInfo.Name); \r\n           } \r\n \r\n           return View(); \r\n       } \r\n<\/string><\/pre>\n

        \"\"<\/p>\n

        \u4f7f\u7528 EmbeddedFileProvider<\/p>\n

        \u987e\u540d\u601d\u4e49\uff0c\u5b83\u5c31\u662f\u7528\u6765\u8bbf\u95ee\u6ce8\u5165\u5230 \u7a0b\u5e8f\u96c6 \u4e2d\u7684\u6587\u4ef6\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5c06 EmbeddedFileProvider \u6ce8\u5165\u5230 ASP.Net Core \u7684\u5bb9\u5668\u4e2d\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n

        \r\npublic void ConfigureServices(IServiceCollection services) \r\n        { \r\n            services.AddControllersWithViews(); \r\n \r\n            IFileProvider embeddedProvider = new EmbeddedFileProvider(Assembly.GetEntryAssembly()) \r\n                ; \r\n            services.AddSingleton(embeddedProvider); \r\n        }\r\n<\/ifileprovider><\/pre>\n

        \u4f7f\u7528 CompositeFileProvider<\/p>\n

        \u770b\u5230 Composite \u5355\u8bcd\uff0c\u662f\u4e0d\u662f\u60f3\u8d77\u4e86\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684 \u7ec4\u5408\u6a21\u5f0f\uff0c\u5bf9\u7684\uff0c\u8fd9\u91cc CompositeFileProvider \u4e5f\u662f\u7c7b\u4f3c\u7684\u64cd\u4f5c\u624b\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u7684 EmbeddedFileProvider \u548c PhysicalFileProvider \u8fdb\u884c\u7ec4\u5408\uff0c\u8fdb\u800c\u5408\u6210\u4e00\u4e2a CompositeFileProvider \u4f9b\u4e0a\u5c42\u8fdb\u884c\u8bbf\u95ee\uff0c\u5982\u679c\u6709\u70b9\u61f5\u7684\u8bdd\uff0c\u53ef\u53c2\u8003\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n

        \r\nvar physicalProvider = _env.ContentRootFileProvider; \r\nvar embeddedProvider = new EmbeddedFileProvider(Assembly.GetEntryAssembly()); \r\nvar compositeProvider = new CompositeFileProvider(physicalProvider, embeddedProvider); \r\n<\/pre>\n

        \u603b\u7684\u6765\u8bf4\uff0c\u4f60\u53ef\u4ee5\u5229\u7528 File Providers \u6765\u83b7\u53d6\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u7684\u4fe1\u606f\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u53d8\u66f4\u901a\u77e5\uff0c\u5982\u679c\u60f3\u5728 ASP.Net Core \u4e2d\u5b9e\u73b0\u6587\u4ef6\u7cfb\u7edf\u8bbf\u95ee\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\u7684\u65b9\u5f0f\u6309\u9700\u4f7f\u7528\u4f60\u7684 File Provider\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

        file prodivers \u5b9e\u73b0\u4e86 IFileProvider \u63a5\u53e3\uff0c\u5b83\u7684\u5b9e\u73b0\u5b50\u7c7b\u5927\u6982\u6709\u4e09\u5757: Physic […]<\/p>\n","protected":false},"author":1329,"featured_media":211406,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-211395","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/211395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=211395"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/211395\/revisions"}],"predecessor-version":[{"id":211400,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/211395\/revisions\/211400"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/211406"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=211395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=211395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=211395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}