-->

The view 'Index' or its master was not fou

2019-01-23 10:39发布

问题:

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.

回答1:

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


回答2:

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.


回答3:

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.

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

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



回答4:

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


回答5:

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.



回答6:

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.



回答7:

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.



回答8:

  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.



回答9:

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.



回答10:

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.



回答11:

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


回答12:

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.



回答13:

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)



回答14:

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.



回答15:

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