当前上下文中不存在名称"查看"

lev*_*nja 32 c# asp.net-mvc

编译时,我收到此错误:The name 'View' does not exist in the current context参考我的代码return View();.

完整示例代码:

namespace Controllers
{
public class FilePageController
{
    //
    // GET: /FilePage/
    public ActionResult Index()
    {
        return View();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

我已经做了几次并且无法在SO上找到答案,所以我想将这个和答案一起发布,以防其他人在学习MVC的同时帮助其他人做了同样的事情.

lev*_*nja 67

控制器不是从controller类继承的.按照惯例,MVC做了很多事情,但最后用"Controller"命名你的类是不够的.

将其更改为public class FilePageController : Controller.

  • 非常感谢!它就像一个魅力!给予好评. (4认同)