搜索视图时可以插入两个MVC视图引擎吗? [英] Can I interleave two MVC view engines when searching for views?

查看:95
本文介绍了搜索视图时可以插入两个MVC视图引擎吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC项目中使用WebForms和Razor视图引擎.他们在一起工作得很好.但是,我建立了一个基于继承的项目,因此我有多个从基础项目派生的子项目.仅使用一个视图引擎,该方法就可以使MVC在子项目中搜索视图,但找不到该视图时,它将搜索基础.

I'm using the WebForms and Razor view engines in my MVC project. They work just fine together. However, I have an inheritance-based project set up, such that I have multiple child projects derived from a base project. With just one view engine, this works such that MVC will search for a view in the child projects, and failing to find it, it will search the base.

但是,当添加第二个视图引擎时,此搜索模式被破坏,以使WebForms引擎先搜索子项,然后搜索基数,然后Razor引擎先搜索子项,然后搜索基数.因此,基本.aspx视图的优先级高于子.cshtml视图.换句话说,当搜索名为MyView的视图时,这是搜索位置的优先列表:

However, when adding the second view engine, this search pattern is broken, such that the WebForms engine searches the child and then the base, and then the Razor engine searches the child and then the base. As such, a base .aspx view will be given priority over a child .cshtml view. In other words, when searching for a view named MyView, this is the prioritized list of locations searched:

Child\MyView.aspx
Base\MyView.aspx
Child\MyView.cshtml
Base\MyView.cshtml

我想要的是让两个引擎分别在检查基础项目之前检查子项目,例如:

What I want is to have the two engines each check the child projects before either checks the base project, as such:

Child\MyView.aspx
Child\MyView.cshtml
Base\MyView.aspx
Base\MyView.cshtml

这有可能吗?如果可以,有人可以向我指出正确的方向吗?

Is this possible, and if so, can someone point me in the right direction?

推荐答案

您可以编写自己的内部使用两个现有视图引擎的视图引擎.

You could write your own view engine that uses both existing view engines internally.

public InterleavedViewEngine : IViewEngine
{
    public string[] SearchLocations;

    RazorViewEngine _razor;
    WebFormsViewEngine _webForms;

    public override ViewEngineResult FindView(...)
    {
        //iterate search paths, trying Razor, then WebForms, etc...
        foreach(var location in SearchLocations)
        {
            var razorResult = _razor.FindView(...);

            if(razorResult.View == null)
            {
               //web forms, etc... here
            }

        }
    }

}

这篇关于搜索视图时可以插入两个MVC视图引擎吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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