gpt4 book ai didi

c# - 如何停止 MVC5 RenderAction 寻找 .aspx 文件

转载 作者:行者123 更新时间:2023-12-04 20:15:06 25 4
gpt4 key购买 nike

我将 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 也能正常工作

任何帮助,将不胜感激

最佳答案

The view '[viewname]' or its master was not found or no view engine support the searched Locations 表示您使用的是默认 View 引擎,它优先考虑 Web 表单 View 引擎(显示为 ~/Areas/NewPages/Views/Shared/_Mainmenu.cshtml.ascx 的路径表示 MVC View 引擎优先搜索 ASPX 和 ASCX 文件而不是 Razor cshtml 文件)。要更改 MVC 默认使用 Razor View 引擎的这种行为,请将这些行插入 Application_Start方法在 Global.asax :

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

此外,如果默认的 Razor View 引擎仍然无法正确识别区域中的 cshtml 文件,则需要创建一个自定义的 View 引擎类,该类继承 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;
}
}

请注意,自定义 View 引擎将根据 AreaViewLocationFormats 中定义的路由在 Controller 操作方法指定的区域内搜索所有 View 页面。 .

然后,在与 RazorViewEngine相同的地方注册自定义 View 引擎类。 ,即在 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?

关于c# - 如何停止 MVC5 RenderAction 寻找 .aspx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45097274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com