未找到"索引"视图或其主页

Bug*_*boo 8 c# razor asp.net-mvc-4

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

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
Run Code Online (Sandbox Code Playgroud)

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

我的RouteConfig.cs内容是:

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

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

我的控制器内容:

 public ActionResult Index()
        {
            return View();
        }
Run Code Online (Sandbox Code Playgroud)

Sho*_*hoe 26

MVC在views文件夹下查找视图(如Index),但它们也必须位于以其控制器命名的文件夹下(除了partial之外的一些例外).

因此,这是您要在项目中遵循的结构

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

Views (folder)
    Home (folder)
        Index (.cshtml)
    Account (folder)
        Index (.cshtml)
Run Code Online (Sandbox Code Playgroud)


Joh*_*ohn 20

我以前曾经遇到过这个问题,它可能是其他任何答案,但它也可能是造成问题的Build Action.

如果右键单击您遇到问题的.cshtml/.vbhtml文件,请选择"属性",然后在属性窗口中将"构建操作"设置为" 内容 ",这样就可以解决您的问题.


IEa*_*els 5

MVC引擎在"共享"下或在名为"控制器类"前缀的文件夹下搜索视图.因此,如果您ABCController需要Index.cshtml在文件夹下查看视图Views/ABC.

PS:在你的例子中你有一个控制器名称后缀(ControllerName),我不认为这是一个好习惯,总是命名你的控制器[Name]Controller