本文介绍了未找到视图“索引"或其主视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C# MVC 项目类型的新手,当我创建一个空的 C# MVC 项目时,我注意到以下错误:

I'm new to the C# MVC project type and when I created an empty C# MVC project, I noticed the following error:

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

我确实在 Views 文件夹下有Index.cshtml"文件.为什么MVC引擎不直接在Views文件夹下查看?我该如何解决这个问题?

I do have the "Index.cshtml" file under the Views folder. Why does the MVC engine not look directly under the Views folder? How do I solve this problem?

我的 RouteConfig.cs 内容是:

My RouteConfig.cs contents are:

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = <ControllerName>, action = "Index", id = UrlParameter.Optional }
            );

我的控制器内容:

 public ActionResult Index()
        {
            return View();
        }

推荐答案

MVC 在 vi​​ews 文件夹下寻找视图(如 Index),但它们也必须在以它们的控制器命名的文件夹下(除了少数例外,如部分).

MVC looks for views (like Index) under the views folder but they also have to be under a folder named after their controller (with a few exceptions like partials).

这就是你想要在你的项目中遵循的结构

So this is the structure you want to follow in your project

Controllers (folder)
    HomeController (.cs)
    AccountController (.cs)

Views (folder)
    Home (folder)
        Index (.cshtml)
    Account (folder)
        Index (.cshtml)

这篇关于未找到视图“索引"或其主视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:09