简体   繁体   中英

Error in creating view in Asp.Net MVC 4

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM