在Asp.Net MVC 4中创建视图时出错

时间:2015-11-11 05:41:41

标签: asp.net-mvc

我是Asp.Net MVC的新手,已被分配了一个项目。我创建了一个名为" webMaster.cshtml"在视图/共享文件夹中。我的控制器名称是:" EmployeeController"和行动名称是:" ViewEmployees"。

public class EmployeeController : Controller
{
    //
    // GET: /Employee/

    public ActionResult ViewEmployees()
    {
        return View("webMaster.cshtml");
    }

}

查看页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title - Practice MVC</title>
</head>
<body>
    <h2>webMaster</h2>
</body>
</html>

运行项目时出错

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

现在,我的问题是为什么要搜索:&#34;〜/ Views / Shared / webMaster.cshtml.cshtml&#34; 相反,它应该搜索&#34;〜/ Views / Shared / webMaster.cshtml&#34;

请帮忙整理一下。

感谢。

2 个答案:

答案 0 :(得分:2)

MVC会接受你的字符串并在错误消息中看到的整个地方查找它。因此,它不希望您指定扩展名。

将其更改为:

File.open

答案 1 :(得分:0)

按照惯例,您应该在视图中名为Employees的文件夹中创建viewemployees.cshtml视图。如果执行此操作,则不必在返回视图语句中指定视图的名称。你只需使用:

return View() ;

MVC将通过查看employee文件夹(假定您的控制器名为EmployeeController)查看名称为viewemployees.cshtml的视图(假设您的操作名为viewemployees)来计算其余部分。

通常最好坚持语言惯例。从长远来看,它使您的生活更轻松,特别是当您的项目最终有数百个视图时。