受欢迎的博客标签

How to access views from a custom folder in ASP.NET Core

Published

https://code.msdn.microsoft.com/How-to-access-views-from-a-0c5e6e9b#content

 


 

How to access views from a custom folder in ASP.NET Core

Introduction

This sample will demonstrate how to access views from custom folder in ASP.NET Core.

Sample prerequisites

• .NET Core 1.0 or later version(s). [.NET Core + Visual Studio tooling]

• Microsoft Visual Studio 2015 update3 or above. [Visual Studio 2015]

Building the sample

• Copy the Views folder to any path in your disk, and copy the new path.

• Open the sample solution “CSAccessViewFromCustomFolder” using Visual Studio.

• Open ”appsettings.json”, local to “ViewRootFolderPath” section, and set the value as the new Views folder path that you have copied.

• Right click the project “CSAccessViewFromCustomFolder” and select Restore packages.

• Press F6 Key or select Build -> Build Solution from the menu to build the sample.

Running the sample

• Open the sample solution using Visual Studio, then press F5 Key or select Debug -> Start Debugging from the menu.

• The sample will be running and loading the view that you have specified.

Using the code

You should implement interface IFileProvider and use it in Startup.

ViewFileProvider.cs

  

C#
Edit|Remove
    public class ViewFileProvider : IFileProvider 
    { 
        private string _viewFolderPath; 
 
        public ViewFileProvider(string viewRootFolder) 
        { 
            _viewFolderPath = viewRootFolder; 
        } 
 
        public IDirectoryContents GetDirectoryContents(string subpath) 
        { 
            var path = ConvertPath(subpath); 
 
            return new ViewDirectoryContents(path); 
        } 
 
        public IFileInfo GetFileInfo(string subpath) 
        { 
            var path = ConvertPath(subpath); 
 
            return new ViewFileInfo(_viewFolderPath + path); 
        } 
 
        public IChangeToken Watch(string filter) 
        { 
            return new NoWatchChangeToken(); 
        } 
 
        private string ConvertPath(string path) 
        { 
            if (path.StartsWith("/Views/", StringComparison.OrdinalIgnoreCase)) 
            { 
                path = path.Substring(7); 
            } 
            if (path.StartsWith("Views/", StringComparison.OrdinalIgnoreCase)) 
            { 
                path = path.Substring(6); 
            } 
            if (path.StartsWith("/", StringComparison.OrdinalIgnoreCase)) 
            { 
                path = path.Substring(1); 
            } 
            return path.Replace("/""\\"); 
        } 
    } 

 

ViewFileInfo.cs

 

C#
Edit|Remove
    public class ViewFileInfo : IFileInfo 
    { 
        private FileInfo _fileInfo; 
        private string _physicalPath; 
 
        public ViewFileInfo(string path) 
        { 
            _physicalPath = path; 
            this._fileInfo = new FileInfo(_physicalPath); 
        } 
 
        public bool Exists 
        { 
            get { return _fileInfo.Exists; } 
        } 
 
        public bool IsDirectory 
        { 
            get { return new DirectoryInfo(_physicalPath).Exists; } 
        } 
 
        public DateTimeOffset LastModified 
        { 
            get { return _fileInfo.LastWriteTime; } 
        } 
 
        public long Length 
        { 
            get { return _fileInfo.Length; } 
        } 
 
        public string Name 
        { 
            get { return _fileInfo.Name; } 
        } 
 
        public string PhysicalPath 
        { 
            get { return this._physicalPath; } 
        } 
 
        public Stream CreateReadStream() 
        { 
            return _fileInfo.OpenRead(); 
        } 
    } 

 

StartUp.cs

 

C#
Edit|Remove
        public void ConfigureServices(IServiceCollection services) 
        { 
            services.AddMvc(); 
 
            services.Configure<RazorViewEngineOptions>(options => 
            { 
                string viewRootPath = Configuration.GetSection("ViewRootFolderPath").Value + "\\"; 
 
                options.FileProviders.Add(new ViewFileProvider(viewRootPath)); 
            }); 
        } 

 

 

Microsoft All-In-One Code Framework is a free, centralized code sample library driven by developers' real-world pains and needs. The goal is to provide customer-driven code samples for all Microsoft development technologies, and reduce developers' efforts in solving typical programming tasks. Our team listens to developers’ pains in the MSDN forums, social media and various DEV communities. We write code samples based on developers’ frequently asked programming tasks, and allow developers to download them with a short sample publishing cycle. Additionally, we offer a free code sample request service. It is a proactive way for our developer community to obtain code samples directly from Microsoft.