Razor 引擎找不到 Index.cshtml 页面,当它明显存在时



我有这个 ASP.NET webapi程序,它运行顺利,帮助页面也可以工作。

突然发生了一些事情,帮助页面将不再加载。

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Areas/HelpPage/Views/Help/Index.aspx
~/Areas/HelpPage/Views/Help/Index.ascx
~/Areas/HelpPage/Views/Shared/Index.aspx
~/Areas/HelpPage/Views/Shared/Index.ascx
~/Views/Help/Index.aspx
~/Views/Help/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Areas/HelpPage/Views/Help/Index.cshtml
~/Areas/HelpPage/Views/Help/Index.vbhtml
~/Areas/HelpPage/Views/Shared/Index.cshtml
~/Areas/HelpPage/Views/Shared/Index.vbhtml
~/Views/Help/Index.cshtml
~/Views/Help/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.InvalidOperationException: The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:

    public ActionResult Index()
    {
        ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
        return View( Configuration.Services.GetApiExplorer().ApiDescriptions);
    }

  public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }

正确的文件清楚地存在于"~/Areas/HelpPage/Views/Help/Index.cshtml"中,我已经按照WebAPI教程注册了所有区域。 视图引擎由于某种未知原因找不到它,帮助??

编辑:

好吧,我有点想通了。当我右键单击ActionResult并单击转到视图时,它给了我一个错误并且什么都不做。

所以,我右键单击ActionResult并添加了一个视图,它在~/Views/Help/Index.cshtml中创建一个新视图

因此,在某种程度上,操作没有指向正确的视图页面,我不知道如何解决这个问题。

我发现了错误,文件夹 Areas 被意外放入错误的目录,而它应该在根目录中

这是因为您在根目录和HelpPage区域中都有一个HelpController,并且视图引擎无法确定要显示哪一个。

请在 AppStart 下打开 RouteConfig.cs 文件,并将根控制器类的命名空间添加到默认路由配置中,如下所示

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { 
                controller = "Home", 
                action = "Index", 
                id = UrlParameter.Optional 
            },
            namespaces: new[] { "yourrootcontrollernamespace" }
        );

查看此优酷视频

相关内容

最新更新