视图“索引”或它的主人未找到 [英] The view 'Index' or its master was not found

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

问题描述

我是新的C#的MVC项目类型,当我创建了一个空的C#MVC项目,我发现了以下错误:

 视图索引或它的主人没有被发现或没有视图引擎支持搜索位置。在以下地点被搜查:
〜/查看/ ControllerName /的Index.aspx
〜/查看/ ControllerName / Index.ascx
〜/查看/共享/的Index.aspx
〜/查看/共享/ Index.ascx
〜/查看/ ControllerName / Index.cshtml
〜/查看/ ControllerName / Index.vbhtml
〜/查看/共享/ Index.cshtml
〜/查看/共享/ Index.vbhtml

我有Views文件夹下的Index.cshtml文件。为什么MVC引擎不要直视视图文件夹下?我该如何解决这个问题?

我的RouteConfig.cs内容是:

  routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);            routes.MapRoute(
                名称:默认,
                网址:{控制器} / {行动} / {ID}
                默认:新{控制器=< ControllerName>中行动=索引,ID = UrlParameter.Optional}
            );

我的控制器的内容:

 公众的ActionResult指数()
        {
            返回查看();
        }


解决方案

MVC查找意见(如索引)下的views文件夹,但他们也必须是自己的控制器命名的文件夹下(像谐音除了少数例外)

所以,这是你想要在你的项目遵循结构

 控制器(文件夹)
    HomeController中(的.cs)
    的AccountController(的.cs)意见(文件夹)
    主页(文件夹)
        指数(.cshtml)
    帐户(文件夹)
        指数(.cshtml)

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

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?

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 }
            );

My controller contents:

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

解决方案

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)

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆