JorgeeFG JorgeeFG - 1 year ago 42
ASP.NET (C#) Question

How to follow what a method does?

I'm new to C# in ASP.Net. I'm using VS 2015 and a NetCore project. I'm used in PHP (PHPStorm IDE) to Control-Click a method name to go to it's definition and see what it does.

In VS 2015, for example for a View() in a Controller, I have to Right Click -> Go to Definition. But the definition is just this:

// Summary:
// Creates a Microsoft.AspNetCore.Mvc.ViewResult object that renders a view to the
// response.
// Returns:
// The created Microsoft.AspNetCore.Mvc.ViewResult object for the response.
public virtual ViewResult View();

So I can't see what it actually does. Is this because it's closed-source?

Answer Source

This isn't because it's closed-source, but rather the code within it has already been compiled and you are simply referencing it through a DLL (and you haven't explicitly loaded the symbols to debug through the actual source).

If you wanted to actually see the chain of method calls and what their source looks like, you can take a look at the ASP.NET Core MVC repository on GitHub or attempt to debug against the source itself as mentioned here.

Your Call Stack for the View() Method

If you wanted to know what actually is going on behind-the-scenes and didn't need to explicitly debug the source itself, you can follow along in the previously linked GitHub repo to see the following calls.

  • View() from Controller.cs

    • Accepts your parameters and passes them to another overload that explicitly takes View/Model parameters.
  • View(name, model) from Controller.cs

    • Builds a ViewResult object using the data from your parameters and the View you are targeting.
  • ViewResult() from ViewResult.cs

    • Pulls in necessary services and resolves the View location, then attempts to execute the action.
  • ExecuteAsync() from ViewResultExecutor.cs

    • Passes all context, view and data along and logs the request being made.
  • ExecuteAsync() from ViewExecutor.cs

    • Ensures that everything is correct and handles building the actual response and flushing it out to the user with the appropriate headers, content, etc.