视图“索引”或者没有找到它的主人。

发布于 2024-08-21 20:06:27 字数 307 浏览 10 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

朱染 2024-08-28 20:06:27

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

例如:

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

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");
桃扇骨 2024-08-28 20:06:27

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

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

  1. 对应的控制器和视图文件夹名称具有相同的名称。
  2. 相应的控制器方法&视图页面应该具有相同的名称。
  3. 如果您的方法名称与视图名称不同,请在方法中返回 view("viewName")

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).

  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.
走野 2024-08-28 20:06:27

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

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

所以,试试这个。

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

示例:假设我们调用 Index() 方法。右键单击它。
输入图像描述这里

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

在此处输入图像描述

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

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

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

So,Try this.

1. Right click your controller method as below.

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

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

enter image description here

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

百合的盛世恋 2024-08-28 20:06:27

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

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

public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "SomeArea_default",
            "SomeArea/{controller}/{action}/{id}",
            new { controller = "SomeController", action = "Index", id = UrlParameter.Optional }
        );
    }
忘羡 2024-08-28 20:06:27

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

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

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

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

An example of how this can happen is if the build action for the views is set to None rather than 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.

掩耳倾听 2024-08-28 20:06:27

问题是我使用 MvcContrib 中的 MvcRoute.MappUrl 来路由 context.Routes

看起来 MvcContrib 路由映射器对区域路由感到不舒服。

The problem was that I used MvcRoute.MappUrl from MvcContrib to route the context.Routes.

It seems that MvcContrib routing mapper was uncomfortable with area routing.

娇柔作态 2024-08-28 20:06:27

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

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

请查看 Phil Haack 的这篇文章

You most likely did not create your own view engine.
The default view engine looks for the views in ~/Views/[Controller]/ and ~/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 post by Phil Haack.

七月上 2024-08-28 20:06:27

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

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.

自由如风 2024-08-28 20:06:27
  1. 请右键单击控制器中的index()方法,
  2. 然后单击goto view

如果此操作打开index.cshtml,

?您的问题是 IIS 池没有权限访问视图的物理路径。

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

  1. right click in index() method from your controller
  2. then click on goto view

if this action open index.cshtml?

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

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

老旧海报 2024-08-28 20:06:27

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

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.

栀子花开つ 2024-08-28 20:06:27

即使您的区域注册中包含所有正确的地图路线,您也可能会收到此错误。尝试将此行添加到您的控制器操作中:

If Not ControllerContext.RouteData.DataTokens.ContainsKey("area") Then
    ControllerContext.RouteData.DataTokens.Add("area", "MyAreaName")
End If

You can get this error even with all the correct MapRoutes in your area registration. Try adding this line to your controller action:

If Not ControllerContext.RouteData.DataTokens.ContainsKey("area") Then
    ControllerContext.RouteData.DataTokens.Add("area", "MyAreaName")
End If
世界和平 2024-08-28 20:06:27

如果您即使在您的区域注册中使用了所有正确的地图路线并且所有其他基本配置都可以得到此错误,也可以得到此错误。

情况是这样的:

我使用了下面提到的 Jquery 文件中的代码来回发数据,然后从控制器操作方法加载视图。

$.post("/Customers/ReturnRetailOnlySales", {petKey: '<%: Model.PetKey %>'}); 

上面的 jQuery 代码我没有提到成功回调函数
发生的事情是在完成操作方法的回发场景后,没有路由到我预期的视图,它返回到 Jquery 端并给出了上面未找到视图的错误。

然后我给出了如下的解决方案并且它的工作没有任何问题。

 $.post("/Customers/ReturnRetailOnlySales", {petKey: '<%: Model.PetKey %>'},
      function (data) {
 var url = Sys.Url.route('PetDetail', { action: "ReturnRetailOnlySalesItems", controller: "Customers",petKey: '<%: Model.PetKey %>'});
 window.location = url;});   

注意:我在成功回调函数中将请求发送到我预期的视图操作方法。然后视图引擎找到相关区域的视图文件并正确加载。

If You can get this error even with all the correct MapRoutes in your area registration and all other basic configurations are fine.

This is the situation:

I have used below mentioned code from Jquery file to post back data and then load a view from controller action method.

$.post("/Customers/ReturnRetailOnlySales", {petKey: '<%: Model.PetKey %>'}); 

Above jQuery code I didn't mentioned success callback function.
What was happened there is after finishing a post back scenario on action method, without routing to my expected view it came back to Jquery side and gave view not found error as above.

Then I gave a solution like below and its working without any problem.

 $.post("/Customers/ReturnRetailOnlySales", {petKey: '<%: Model.PetKey %>'},
      function (data) {
 var url = Sys.Url.route('PetDetail', { action: "ReturnRetailOnlySalesItems", controller: "Customers",petKey: '<%: Model.PetKey %>'});
 window.location = url;});   

Note: I sent my request inside the success callback function to my expected views action method.Then view engine found a relevant area's view file and load correctly.

心不设防 2024-08-28 20:06:27

我也遇到过这个问题;我注意到我错过了将视图页面包含在与控制器名称相同的文件夹中。

控制器:adminController
View->Admin->view1.cshtml

(原来是View->view1.cshtml)(没有文件夹:Admin)

I have had this problem too; I noticed that I missed to include the view page inside the folder that's name is same with the controller.

Controller: adminController
View->Admin->view1.cshtml

(It was View->view1.cshtml)(there was no folder: Admin)

年少掌心 2024-08-28 20:06:27

如果您的 MSI 安装程序未能实际部署该文件,也可能会出现此错误。

就我而言,发生这种情况是因为我将 .aspx 文件转换为 .cshtml 文件,而 Visual Studio 认为这些是全新文件,并将构建操作设置为 none 而不是 content。

This error can also surface if your MSI installer failed to actually deploy the file.

In my case this happened because I converted the .aspx files to .cshtml files and visual studio thought these were brand new files and set the build action to none instead of content.

幸福丶如此 2024-08-28 20:06:27

我在这里遇到了同样的问题,猜猜看……查看 csproj 的 xml' 结构,我注意到内容节点(在 ItemGroup 节点内)为“无”……不知道为什么,但这就是我的原因遇到了同样的错误,只需将其编辑为“内容”,就像其他错误一样,它就可以工作了。

希望有帮助

I got the same problem in here, and guess what.... looking at the csproj's xml' structure, I noticed the Content node (inside ItemGroup node) was as "none"... not sure why but that was the reason I was getting the same error, just edited that to "Content" as the others, and it's working.

Hope that helps

高跟鞋的旋律 2024-08-28 20:06:27

在项目内的 Application_Start() 方法中添加以下代码:

ViewEngines.Engines.Add(new RazorViewEngine());

Add the following code in the Application_Start() method inside your project:

ViewEngines.Engines.Add(new RazorViewEngine());
伤痕我心 2024-08-28 20:06:27

我将 viewlocationformat 添加到 RazorViewEngine 并为我工作。

ViewLocationFormats = new[] {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml",
            "~/Areas/Admin/Views/{1}/{0}.cshtml",
            "~/Areas/Admin/Views/Shared/{0}.cshtml"
        };

I added viewlocationformat to RazorViewEngine and worked for me.

ViewLocationFormats = new[] {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml",
            "~/Areas/Admin/Views/{1}/{0}.cshtml",
            "~/Areas/Admin/Views/Shared/{0}.cshtml"
        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文