在ASP.NET Core MVC中,是否可以从项目文件夹外部添加View文件夹? [英] in ASP.NET Core MVC, is it possible to add a View folder from outside the project folder?

查看:350
本文介绍了在ASP.NET Core MVC中,是否可以从项目文件夹外部添加View文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的大多数视图都是普通/Views/<Controller>文件夹层次结构中的常规<action>.cshtml文件.这些是在git中由源代码控制的,并以通常的翻录和替换"方式进行部署.

The majority of my views are regular <action>.cshtml files in the normal /Views/<Controller> folder hierarchy. These are source controlled in git and deployed in the usual "rip and replace" way.

但是,我也使用Razor渲染模板来创建HTML电子邮件,并且电子邮件.cshtml模板特定于每个客户端.因此,我希望能够从Application Root文件夹的外部加载和呈现它们,以便在部署过程中不会丢失特定于客户端的自定义项.

However I also use Razor for rendering templates to create HTML emails, and the email .cshtml templates are specific to each client. I therefore want to be able to load and render them from outside the Application Root folder, so the client-specific customisations are not lost during deployment.

我已经成功创建并注册了IViewLocationExpander界面的实现,并且适用于应用程序根目录下的文件夹:

I have successfully created and registered an implementation of the IViewLocationExpander interface and this works for folders inside the Application Root:

public class EmailViewLocationExpander : IViewLocationExpander
{
    protected readonly String _TemplateFolder;

    public EmailViewLocationExpander( String TemplateFolder )
    {
        _TemplateFolder = TemplateFolder.Trim('/');
    }

    public void PopulateValues( ViewLocationExpanderContext context )
    {
    }

    public IEnumerable<string> ExpandViewLocations( ViewLocationExpanderContext context, IEnumerable<string> viewLocations )
    {
        var result = new List<String>( viewLocations );

        result.Add( $"/{ _TemplateFolder }/Email/{{0}}.cshtml" );
        result.Add( $"/{ _TemplateFolder }/Shared/{{0}}.cshtml" );

        return result;
    }
}

它似乎不适用于除Application Root相对路径以外的其他路径,例如/../Templates似乎不起作用.

It does not seem to work for paths other than Application Root relative, so e.g. /../Templates doesn't seem to work.

我目前还依赖于自定义_ViewStart.cshtml作为我的电子邮件模板,并阅读

I also currently rely on having a custom _ViewStart.cshtml for my email templates, and reading the Mvc source code leads me to think that my only option is to implement a custom IFileProvider to reference the physical file system outside the current Application Root - is that right and can anyone help me with an example if it is?

推荐答案

在我看来,这很对. ViewLocationExpander处理相对于wwwroot文件夹的路径,因此对于指定该目录之外的文件的路径将无用.

That seems right to me. The ViewLocationExpander deals with paths relative to the wwwroot folder so it's not going to be useful for specify paths to files outside of that.

这是一篇有关实现IFileProvider的不错的文章.这些文章演示了如何创建和IFileProvider来访问存储在数据库中的视图,但是从文件系统中读取它们将更加容易.因此,这是要考虑的内容的概要. https://www.mikesdotnetting.com/article/301/loading-asp-net-core-mvc-views-from-a-database-or-other-location

Here is a pretty good article on implementing an IFileProvider. The articles demonstrates creating and IFileProvider for accessing views stored in a database but reading them from the file system will be even easier. So it's a great outline of what to consider. https://www.mikesdotnetting.com/article/301/loading-asp-net-core-mvc-views-from-a-database-or-other-location

我注意到的一件事是IFileProvider的工作方式非常酷.您将自定义实现注册为RazorViewEngine的选项,如下所示,当ViewEngine需要获取文件时,它将依次向每个FileProvider询问该文件,直到该文件返回为止.

One thing I noted is that the way the way the IFileProvider works is pretty cool. You register your custom implementation as an option to the RazorViewEngine like below and when the ViewEngine needs to get a file, it asks each FileProvider, in order, for that file until one returns it.

 public void ConfigureServices(IServiceCollection services)
 {
    string customLocation = @"c:\mylocation\"

    // Add framework services.
    services.AddMvc();
    services.Configure<RazorViewEngineOptions>(opts => 
        opts.FileProviders.Add(
            new MyCustomFileProvider(customLocation)
        )
    );
 }

因此,最后,您基本上只实现了FileProvider即可为某些特定的url端点提供文件.您可以从任何喜欢的地方获取它们.

So in the end, you basically just implement your FileProvider to provide files for some specific url endpoints. You can obtain them from anyplace you like.

这篇关于在ASP.NET Core MVC中,是否可以从项目文件夹外部添加View文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆