The view 'Index' or its master was not fou

2020-02-01 02:25发布

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

9条回答
叛逆
2楼-- · 2020-02-01 02:41

You may have old/bad ASP.Net assemblies in your build.

If you've made sure you have your Controller, namespace and View names all in the right place, there's a good chance your MVC/ASP Assembly is having problems working with the current Mono environment.

If you've tried the tricks to copy all the .Net assemblies you can find and tag them into your bin, there's a good chance Mono or its configuration is not melding correctly - and you don't need to do that anymore. I had this problem when updating a machine to the latest Mono build. I wiped these assemblies from my bin, and NuGet'ed the latest assemblies in the Mono MVC Packages folder.

First thing you'll see is:

  • Microsoft.AspNet.Mvc
  • Microsoft.AspNet.Infrastructure
  • Microsoft.AspNet.WebPages
  • Microsoft.AspNet.Razor

This will resolve these problems.

查看更多
做自己的国王
3楼-- · 2020-02-01 02:43

When a view is returned, it expects that an associated .cshtml file is in the same view folder structure as the controller layout for that area (if no areas are in use, then there is only 1 view folder and 1 controller folder). The controller name will be the folder name in the views folder, and the actionresult name will be the expected name of the .cshtml file.

Luckily there is an easy way to remedy the situation where the view file is missing. Right click on Index for your action result, and then select Add View. Click okay, and it will create Index.cshtml for you inside of the correct folder. Now when you run the project, and navigate to Index, that is what you will see.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-02-01 02:43

I had a virtual directory that wasn't correctly referencing the actual directory. (On Windows) I fixed this by going into IIS (Internet Information Services Manager), and right clicking the virtual directory that wasn't being correctly referenced (In your case, Views). Virtual directories will have this little icon next to them:

enter image description here

Right click and then select Manage Virtual Directory > Advanced Settings. Ensure the Physical path matches where you think it should be going:

enter image description here

This isn't going to apply to all cases, but it's a possible match for some instances.

查看更多
走好不送
5楼-- · 2020-02-01 02:44

I've had this problem a few times before and it can be any of the other answers, but it can also be the Build Action that is causing the issue.

If you right click on the .cshtml/.vbhtml file that you are having a problem with, select Properties, and in the properties window set the Build Action to Content then this can be a solution to your problem.

查看更多
对你真心纯属浪费
6楼-- · 2020-02-01 02:44

If you manually deployed the code to an instance of IIS, make sure that the App Pool Identity that is being used to for that application in IIS has rights to the folders in the application.

A good way to diagnose if it's something wrong with your code or something wrong with your IIS settings is to run it in the VS 201X debugger. If it runs there, but won't run under IIS directly, it's a good chance that the App Pool Identity security is jacked up.

查看更多
神经病院院长
7楼-- · 2020-02-01 02:44

I know this is an old post but I ran into this same situation running on Ubuuntu 16.04 and Mono 4.2.1 and none of this helped. I started digging into other things and after a week I found out that for some reason Microsoft.Web.WebPages.OAuth.dll was causing this error. after deleting that dll and removing all refrenses to it (I'm not using the open auth anyway) everything immediately started working even using simple membership with MySQL. So I wanted to post this here in case someone else runs into this same situation on Mono and Linux because this comes up in google.

查看更多
登录 后发表回答