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

C#asp.net Mvc

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlevininjaView Question on Stackoverflow
Solution 1 - C#levininjaView Answer on Stackoverflow
Solution 2 - C#Farukh ShaikhView Answer on Stackoverflow