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();
}
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
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 yourIndex.cshtml
view under the folderViews/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
A useful diagnostic step is to right click inside the Controller's Action, choose "Go To View".
If Visual Studio can find the view, then it's probably in the right folder, so re-check your URL. For example, if using an Area then the area name should be in the URL: