asp.net-mvc 未找到视图“索引”或其主视图。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2269220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:08:28  来源:igfitidea点击:

The view 'Index' or its master was not found.

asp.net-mvcasp.net-mvc-routingasp.net-mvc-areas

提问by Fitzchak Yitzchaki

The view 'Index' or its master was not found. The following locations were searched:
~/Views/ControllerName/Index.aspx
~/Views/ControllerName/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx

I got this error when using ASP.Net mvc area. The area controller action are invoked, but it seems to look for the view in the 'base' project views instead of in the area views folder.

使用 ASP.Net mvc 区域时出现此错误。调用区域控制器操作,但它似乎在“基本”项目视图中而不是在区域视图文件夹中查找视图。

采纳答案by Fitzchak Yitzchaki

The problem was that I used MvcRoute.MappUrlfrom MvcContribto route the context.Routes.

问题是我用MvcRoute.MappUrlfromMvcContrib来路由context.Routes.

It seems that MvcContribrouting mapper was uncomfortable with area routing.

MvcContrib路由映射器似乎对区域路由感到不舒服。

回答by Shannon Deminick

What you need to do is set a token to your area name:

您需要做的是为您的区域名称设置一个令牌:

for instance:

例如:

context.MapRoute(
        "SomeArea_default",
        "SomeArea/{controller}/{action}/{id}",
        new { controller = "SomeController", action = "Index", id = UrlParameter.Optional }
    ).DataTokens.Add("area", "YOURAREANAME");

回答by Sai Sherlekar

This error was raised because your Controller method name is not same as the View's name.

引发此错误是因为您的控制器方法名称与视图名称不同。

If you right click on your controller method and select Go To View (Ctrl+M,Ctrl+G), it will either open a View (success) or complain that it couldn't find one (what you're seeing).

如果您右键单击您的控制器方法并选择 Go To View (Ctrl+M,Ctrl+G),它将打开一个视图(成功)或抱怨它找不到一个(您所看到的)。

  1. Corresponding Controllers and View folders name have the same names.
  2. Corresponding Controller methods & Views pages should same have the same names.
  3. If your method name is different than view name, return view("viewName")in the method.
  1. 对应的控制器和视图文件夹名称具有相同的名称。
  2. 相应的控制器方法和视图页面应该具有相同的名称。
  3. 如果您的方法名称与视图名称不同,则return view("viewName")在方法中。

回答by Elshan

Global.asax file contain the URL Route. Default URL route like this.

Global.asax 文件包含 URL 路由。像这样的默认 URL 路由。

"{controller}/{action}/{id}"

So,Try this.

所以,试试这个。

1. Right click your controller method as below.

1. 右键单击​​您的控制器方法,如下所示。

Example: let say we call Index()method.Right click on it. enter image description here

示例:假设我们调用方法Index()。右键单击它。 在此处输入图片说明

2. Click Add View.. and give appropriate name.In this example name should be Index.

2. 单击添加视图.. 并提供适当的名称。在此示例中,名称应为索引。

enter image description here

在此处输入图片说明

Then it will add correct View by creating with relevant folder structure.

然后它将通过使用相关文件夹结构创建来添加正确的视图。

回答by Raphael

Check the generated code at MyAreaAreaRegistration.cs and make sure that the controller parameter is set to your default controller, otherwise the controller will be called bot for some reason ASP.NET MVC won't search for the views at the area folder

检查MyAreaAreaRegistration.cs 中生成的代码并确保控制器参数设置为您的默认控制器,否则控制器将因某种原因被称为 bot ASP.NET MVC 不会在区域文件夹中搜索视图

public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "SomeArea_default",
            "SomeArea/{controller}/{action}/{id}",
            new { controller = "SomeController", action = "Index", id = UrlParameter.Optional }
        );
    }

回答by Scott Munro

Where this error only occurs when deployed to a web server then the issue could be because the views are not being deployed correctly.

如果此错误仅在部署到 Web 服务器时发生,则问题可能是因为未正确部署视图。

An example of how this can happen is if the build action for the views is set to None rather than Content.

发生这种情况的一个例子是,如果视图的构建操作设置为 None 而不是 Content。

A way to check that the views are deployed correctly is to navigate to the physical path for the site on the web server and confirm that the views are present.

检查视图是否正确部署的一种方法是导航到 Web 服务器上站点的物理路径并确认视图存在。

回答by ?a?da? Tekin

You most likely did not create your own view engine.
The default view engine looks for the views in ~/Views/[Controller]/and ~/Views/Shared/.

您很可能没有创建自己的视图引擎。
默认视图引擎在~/Views/[Controller]/和 中查找视图~/Views/Shared/

You need to create your own view engine to make sure the views are searched in area views folder.

您需要创建自己的视图引擎以确保在区域视图文件夹中搜索视图。

Take a look this postby Phil Haack.

看看Phil Haack 的这篇文章

回答by Beruk Berhane

  1. right click in index()method from your controller
  2. then click on goto view
  1. 右键单击index()控制器中的方法
  2. 然后点击 goto view

if this action open index.cshtml?

如果这个动作打开index.cshtml?

Your problem is the IIS pool is not have permission to access the physical path of the view.

您的问题是 IIS 池无权访问视图的物理路径。

you can test it by giving permission. for example :- go to c:\inetpub\wwwroot\yourwebthen right click on yourweb folder -> property ->security and add group name everyone and allow full control to your site . hope this fix your problem.

您可以通过授予权限进行测试。例如:-转到c:\inetpub\wwwroot\yourweb然后右键单击 yourweb 文件夹-> 属性-> 安全性并添加组名所有人并允许完全控制您的站点。希望这能解决您的问题。

回答by Greg Grater

I had this problem today with a simple out of the box VS 2013 MVC 5 project deployed manually to my local instance of IIS on Windows 8. It turned out that the App Pool being used did not have the proper access to the application (folders, etc.). After resetting my App Pool identity, it worked fine.

我今天遇到了这个问题,一个简单的开箱即用的 VS 2013 MVC 5 项目手动部署到我在 Windows 8 上的本地 IIS 实例。事实证明,正在使用的应用程序池没有正确访问应用程序(文件夹,等等。)。重置我的应用程序池身份后,它工作正常。

回答by Michael Castro

It′s still a problem on the Final release.. .when you create the Area from context menu/Add/Area, visual studio dont put the Controller inside de last argument of the MapRoute method. You need to take care of it, and in my case, I have to put it manually every time I create a new Area.

在最终版本中仍然存在问题...当您从上下文菜单/添加/区域创建区域时,visual studio 不会将控制器放在 MapRoute 方法的最后一个参数中。你需要照顾它,就我而言,我每次创建新区域时都必须手动放置它。