Reputation: 99
I am new in Asp.Net MVC and has been assigned a project. I have created a view named as "webMaster.cshtml" in Views/Shared folder. My controller name is: "EmployeeController" and Action name is: "ViewEmployees".
public class EmployeeController : Controller
{
//
// GET: /Employee/
public ActionResult ViewEmployees()
{
return View("webMaster.cshtml");
}
}
View Page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title - Practice MVC</title>
</head>
<body>
<h2>webMaster</h2>
</body>
</html>
Error while running the project
Server Error in '/' Application.
The view 'webMaster.cshtml' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Employee/webMaster.cshtml.aspx
~/Views/Employee/webMaster.cshtml.ascx
~/Views/Shared/webMaster.cshtml.aspx
~/Views/Shared/webMaster.cshtml.ascx
~/Views/Employee/webMaster.cshtml.cshtml
~/Views/Employee/webMaster.cshtml.vbhtml
~/Views/Shared/webMaster.cshtml.cshtml
~/Views/Shared/webMaster.cshtml.vbhtml
Now, my question is why it is searching for: "~/Views/Shared/webMaster.cshtml.cshtml" Instead, it should search for "~/Views/Shared/webMaster.cshtml"
Please help in sorting this out.
Thanks.
Upvotes: 0
Views: 982
Reputation: 1808
By convention you should be creating a viewemployees.cshtml view within a folder called Employees within views. If you do this, you won't have to specify the name of the view in the return view statement. You would just use:
return View() ;
MVC will work out the rest by looking in the employee folder (given that your controller is called EmployeeController) for view with name viewemployees.cshtml (given that your action is called viewemployees).
It's generally best to stick to the language conventions. It makes life easier for you in the long run, especially when your project ends up with hundreds of views.
Upvotes: 0
Reputation: 1593
MVC will take your string and look for it in a whole whack of places like you can see in the error message. As such, it doesn't want you to dictate the extension.
Change it to:
public ActionResult ViewEmployees()
{
return View("webMaster");
}
Upvotes: 2