【问题标题】:How do I stop MVC5 RenderAction looking for .aspx files如何停止 MVC5 RenderAction 寻找 .aspx 文件
【发布时间】:2018-06-08 10:40:15
【问题描述】:

我将 MVC 添加到我现有的 webforms 项目中。一切顺利,除了 RenderAction 正在寻找 .aspx 文件

The view '_Mainmenu.cshtml' or its master was not found or no view engine supports the searched locations. The following locations were searched:

~/Areas/NewPages/Views/Shared/_Mainmenu.cshtml.ascx

视图是

~/Areas/NewPages/Views/Shared/_Mainmenu.cshtml

而且它确实存在于该文件夹中。谁能帮我解决这个问题。 其他一切 MVC 都可以正常工作我什至 PITA EntityFramework 也可以工作

任何帮助将不胜感激

【问题讨论】:

  • 确保 Razor 引擎首先由 ViewEngines.Engines.Add(new RazorViewEngine()); 加载,然后是 ViewEngines.Engines.Add(new WebFormViewEngine());。为什么使用双 cshtml + ascx 扩展进行用户控制?如果您想从 MVC 视图搜索中删除 Web 表单引擎,请使用 ViewEngines.Engines.Clear() & 然后添加 ViewEngines.Engines.Add(new RazorViewEngine());
  • @TetsuyaYamamoto 我没有使用双重扩展。该视图名为..._Mainmenu.cshtml,应用程序正在寻找..._Mainmenu.cshtml.aspx,因为它不存在,所以它永远不会找到。请把ViewEngines.Engines.Add(new RazorViewEngine());ViewEngines.Engines.Add(new WebFormViewEngine()); 放在哪里。我搜索了ViewEngines,但在我的项目中找不到该术语
  • 嗯,默认情况下,MVC 优先考虑 Web 表单视图引擎(ASPX 文件),因此您需要通过将这些行放在 Application_Start 中来从 MVC 上下文中删除 Web 表单视图引擎支持:ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new RazorViewEngine());
  • @TetsuyaYamamoto 感谢我无意中解决了这个问题,现在我得到了这个文件名~/Areas/NewPages/Views/Default/_Mainmenu.cshtml.cshtml
  • 你现在遇到双cshtml扩展了吗?如果是真的,从RazorEngine 创建一个类(例如public class CustomRazorEngine : RazorViewEngine)并在构造函数中声明AreaViewLocationFormatsAreaViewLocationFormats = new[] { "~/Areas/{2}/Views/{1}/{0}.cshtml" }。然后在Application_Start注册自定义视图:ViewEngines.Engines.Add(new CustomRazorEngine());

标签: c# asp.net asp.net-mvc


【解决方案1】:

未找到视图“[viewname]”或其主视图,或者没有视图引擎支持搜索到的位置表示您正在使用默认视图引擎,该引擎优先考虑 Web 表单视图引擎(显示的路径~/Areas/NewPages/Views/Shared/_Mainmenu.cshtml.ascx 表示 MVC 视图引擎优先搜索 ASPX 和 ASCX 文件而不是 Razor cshtml 文件)。要更改 MVC 默认使用 Razor 视图引擎的这种行为,请将这些行插入到 Global.asax 上的 Application_Start 方法中:

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
// ViewEngines.Engines.Add(new WebFormViewEngine()); => optional webforms engine registration

另外,如果默认的 Razor 视图引擎仍然无法正确识别区域中的 cshtml 文件,您需要创建一个继承 RazorViewEngine 的自定义视图引擎类,并在其构造函数中设置 AreaViewLocationFormats,如下所示:

public class CustomViewEngine : RazorViewEngine
{
    public CustomViewEngine()
    {
        // Route parsing convention for view engines:
        // {0} means action method name
        // {1} means controller class name
        // {2} means area name

        AreaMasterLocationFormats = new[] 
        {
            "~/Areas/{2}/Views/Shared/{0}.cshtml"
        };

        AreaViewLocationFormats = new[] 
        {
            "~/Areas/{2}/Views/{1}/{0}.cshtml",

            // other view search locations here
        };

        AreaPartialViewLocationFormats = AreaViewLocationFormats;
    }
}

请注意,自定义视图引擎将根据 AreaViewLocationFormats 中定义的路由搜索控制器操作方法指定的区域内的所有视图页面。

然后,在RazorViewEngine的同一位置注册自定义视图引擎类,即Global.asax

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    // clear all view engines repository first
    ViewEngines.Engines.Clear();

    // register Razor view engine only
    ViewEngines.Engines.Add(new RazorViewEngine());

    // register custom view engine class here
    ViewEngines.Engines.Add(new CustomViewEngine());

    // other initialization codes here
}

类似问题:

ASP.NET MVC: When should I create custom View Engine

How do I implement a custom RazorViewEngine to find views in non-standard locations?

【讨论】:

    【解决方案2】:

    在“Global.asax”文件中,找到“Application_Start()”

    那么,请输入此代码--

    RemoveWebFormEngines();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-26
      • 2022-12-05
      • 1970-01-01
      • 2011-04-28
      • 1970-01-01
      • 2019-04-10
      • 2011-06-03
      • 1970-01-01
      相关资源
      最近更新 更多