Click here to Skip to main content
15,033,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is just a regular code of MVC where we are invoking View() function. If I go to the definition of View(), I do not see any static keyword in the definition.

So How is it possible to call view method in a static way ? It should have been something like View vw = new View()

public class HomeController : Controller
   {
       public IActionResult Index()
       {
           return View();
       }

       public IActionResult Secret()
       {
           return View();
       }
   }


What I have tried:

Just googled and googled and googled. :)
Posted
Updated 3-Jun-21 3:58am

It's not a static method; it's an instance method defined on the base class.

Your code is equivalent to calling:
C#
return this.View();
But the this. prefix is unnecessary, so it's usually omitted.
   
View() is not a class. It's not static.

Like Richard said, it's an instance method found in the Controller class that your controller class inherits from.

All the View() method does is call an internal View method that takes 3 parameters, all of which are null, a viewName, a masterName, and a model. This method creates a new ViewResult object and returns it.

Through a convoluted chain of method calls and classes, ViewResult will use the current ControllerContext to lookup a view (Razor page) with the same name as the controller it's being called from. That Razor page is then interpreted and used to build out the HTML sent back to the client.

That's what is being returned by View().
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900