如何阻止MVC5 RenderAction查找.aspx文件

时间:2017-07-14 07:35:04

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

我将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工作

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

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

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?

答案 1 :(得分:0)

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

然后,请输入此代码 -

RemoveWebFormEngines();