levininja
levininja

Reputation: 3258

The name 'View' does not exist in the current context

When compiling, I get this error: The name 'View' does not exist in the current context in reference to my code return View();.

Full example code:

namespace Controllers
{
public class FilePageController
{
    //
    // GET: /FilePage/
    public ActionResult Index()
    {
        return View();
    }
}
}

I have done this a couple times and been unable to find an answer on SO, so I wanted to post this along with the answer, in case it helps others who have done the same thing as me while learning MVC.

Upvotes: 63

Views: 41686

Answers (2)

Farukh Shaikh
Farukh Shaikh

Reputation: 89

  1. Web Api doesnt contain View so when you create new class inside action method return View() use inherited Controller not ControllerBase

Upvotes: 2

levininja
levininja

Reputation: 3258

The controller is not inheriting from the controller class. MVC does many things by convention, but naming your class with "Controller" on the end is not enough.

Change it to public class FilePageController : Controller.

By the way, Controller class inherits from ControllerBase class. Hence, members of ControllerBase class are accessible from class inherited from Controller class.

Upvotes: 122

Related Questions