简体   繁体   English

在ActionFilterAttribute.OnActionExecuted中设置布局是有问题的

[英]Setting Layout in ActionFilterAttribute.OnActionExecuted is problematic

I'm trying to set the layout path in a custom ActionFilterAttribute I have written as follow: 我正在尝试在我编写的自定义ActionFilterAttribute设置布局路径,如下所示:

public class LayoutInjecterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);
        var result = filterContext.Result as ViewResult;
        if (result != null)
        {
            result.MasterName = "~/Views/Layouts/Test.cshtml"
        }
    }
}

In here Test.cshtml is precompiled view (with the help of RazorGenerator ) in a different project. 在这里Test.cshtml是预编译的视图(的帮助下RazorGenerator在不同的项目)。

But it gives me the error: 但它给了我错误:

The view 'Index' or its master was not found or no view engine supports the searched locations. 未找到视图“索引”或其主数据或视图引擎不支持搜索的位置。 The following locations were searched: ~/Views/Home/Index.cshtml ~/Views/Shared/Index.cshtml ~/Views/Home/Index.aspx ~/Views/Home/Index.ascx ~/Views/Shared/Index.aspx ~/Views/Shared/Index.ascx ~/Views/Layouts/Test.cshtml 搜索了以下位置:〜/ Views / Home / Index.cshtml~ / Views / Shared / Index.cshtml~ / Views / Home / Index.aspx~ / Views / Home / Index.ascx~ / Views / Shared / Index。 aspx~ / Views / Shared / Index.ascx~ / Views / Layouts / Test.cshtml

and controller actually is simple: 和控制器实际上很简单:

[LayoutInjecter]
public class HomeController : Controller
{
    public ActionResult Index()
    {
       return View();
    }
}

The error shows that LayoutInjecter is working fine. 错误显示LayoutInjecter工作正常。 You said: 你说:

In here Test.cshtml is precompiled view in a different project. 在这里,Test.cshtml是在不同项目中的预编译视图。

But , Using razor views from a different (from outside the web project) is not supported out of the box. 但是 ,不支持使用来自不同(来自Web项目外部)的剃刀视图。 However there's a tool to precompile razor views and then you can put them in any DLL which called RazorGenerator . 然而,有一个预编译剃刀视图的工具,然后你可以把它们放在任何调用RazorGenerator的 DLL中。 The compiler can't find the specified master layout file and shows this error. 编译器找不到指定的主布局文件并显示此错误。

For more information look at 有关更多信息,请查看

Edit: How did the PrecompiledMvcViewEngine know which view to render? 编辑:PrecompiledMvc​​ViewEngine如何知道要渲染哪个视图?

PrecompiledMvcViewEngine still relies on the ASP.NET MVC Views folder convention , using relative file paths to locate the views. PrecompiledMvcViewEngine 仍然依赖于ASP.NET MVC Views文件夹约定 ,使用相对文件路径来定位视图。 However, this is slightly misleading. 但是,这有点误导。 The PrecompiledMvcViewEngine doesn't look at physical files ; PrecompiledMvcViewEngine 不查看物理文件 ; it looks for the System.Web.WebPages.PageVirtualPathAttribute that the Razor Single File Generator adds to every view that it generates that includes the view's relative file path. 它查找Razor单文件生成器添加到它生成的每个视图的System.Web.WebPages.PageVirtualPathAttribute ,其中包含视图的相对文件路径。

Edit 2: I believe the guidance for your problem would be found in GitHub . 编辑2:我相信您的问题的指导将在GitHub找到

It works. 有用。 Make sure the layout path "~/Views/Layouts/Test.cshtml" is correct. 确保布局路径"~/Views/Layouts/Test.cshtml"正确无误。

Also, make sure that "Test.cshtml" is a layout page, and not a view / partial view. 另外,请确保“Test.cshtml”是布局页面,而不是视图/局部视图。

Change result.MasterName = "~/Views/Layouts/Test.cshtml" to result.MasterName ="~/Views/Shared/Test.cshtml" . result.MasterName = "~/Views/Layouts/Test.cshtml"更改为result.MasterName ="~/Views/Shared/Test.cshtml" The Framework by convention looks in the ~/Views/Shared/ directory in your asp.net mvc solution for your layout pages. 按照惯例,框架在您的asp.net mvc解决方案的〜/ Views / Shared /目录中查找布局页面。 It appears to me you are dynamically or at runtime selecting a master page. 在我看来,您是动态地或在运行时选择母版页。

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

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