0

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.

2 Answers 2

2

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");
}
Sign up to request clarification or add additional context in comments.

6 Comments

Ok. Thanks. It worked. My mistake asking such stupid questions.
@Agnib, You have asked multiple questions on this site, and there are some answers which worked for your and need to be accepted!
How to accept an answer over here? I cannot find any link or something
@Agnib There should be a check mark underneath the voting buttons to the left that is greyed out. You click it to accept an answer.
Hmm, I did it. Thanks. @Runesun
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.