繁体   English   中英

结合使用Custom和Razor时将引擎混乱

[英]Custom and Razor view engine mess while combining the two

我有一个ASP.NET Web API应用程序,该应用程序还呈现了一些剃须刀页面。
默认情况下,有两个默认引擎(webform \\ razor),渲染剃刀页面没有问题。

现在,我需要支持使用自定义引擎呈现的一些旧的aspx \\ ascx页面。

因此,当我引导应用程序时,我将执行以下操作:

    // Remove the default web form engine
    ViewEngines.Engines.Remove(ViewEngines.Engines.OfType<WebFormViewEngine>().FirstOrDefault());
    // Add my custom engine
    ViewEngines.Engines.Add(ApplicationContainer.Resolve<CustomViewEngine>());

我的自定义视图引擎的原型是:

public class CustomViewEngine: VirtualPathProviderViewEngine

现在,我的问题是旧的剃须刀页面,由于某些原因(而不是剃须刀视图引擎),它们使用此引擎呈现,并且在“ FindView”功能运行时出现异常。

我以特殊的方式渲染了我的剃须刀页面,但最重要的是,它看起来像这样:

public ActionResult MyAction()
{
   return View('Razor/abcd.cshtml',model);
}

我已经阅读过Web表单引擎首先运行,并且只有在运行剃须刀之后才运行,但是我不确定那是正确的。
我试图从自定义引擎返回null和其他东西,但是页面无法呈现。

为什么我的.cshtml路径使用自定义引擎而不是剃刀引擎呈现?
如何告诉自定义引擎传递以.cshtml结尾的文件?

好的,我找到了一种解决方法-从“ FileExists”函数返回false将转到下一个文件引擎:

 protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
        {
            try
            {
                return !virtualPath.EndsWith("cshtml") &&
                    File.Exists(controllerContext.HttpContext.Server.MapPath(virtualPath));
            }
            catch (HttpException exception)
            {
                if (exception.GetHttpCode() != 0x194)
                {
                    throw;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM