Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The view 'Index' or its master was not found

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();
        }
like image 957
Bugaboo Avatar asked Aug 08 '14 18:08

Bugaboo


4 Answers

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)
like image 115
James Sampica Avatar answered Nov 08 '22 18:11

James Sampica


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.

like image 31
John Avatar answered Nov 08 '22 18:11

John


The MVC engine search for a view under Shared or under the folder that is named like the prefix of your controller class. So if you have ABCController you need to have your Index.cshtml view under the folder Views/ABC.

PS : In your example you have a suffix to your controller name (ControllerName), I don't think it is a good practice, always name your controllers [Name]Controller

like image 43
IEatBagels Avatar answered Nov 08 '22 19:11

IEatBagels


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.

like image 2
Travis J Avatar answered Nov 08 '22 17:11

Travis J