How to find references for MVC controller methods in Visual Studio

Beginning with a long period of WebForms development, I recently started working with MVC. One thing that struck me is that since controller methods are not called directly from code, Visual Studio will not get references to them.

Silly picture of example code

I understand the reason for the lack of a reference account, but sometimes it can be problematic. Let's say I rewrite a controller method to accommodate some new requirement, perhaps the method needs additional data or a modified result for a specific call. But maybe this method or endpoint if you like is being called from multiple places and the change I made breaks the processing of the results for these other calls. I would like to be able to easily find all the code in my solution that will call the endpoint.

I currently prefer to copy the method name and do regular text search throughout the solution, which works pretty well as long as the method name is not too general. If the method name is "Index" it can be a long day.

Are there any recommendations on how to simplify this, like coding conventions, plugins or other? those. how can i find where the endpoint will be called from?

I am currently using Visual Studio 2017 Enterprise, but solutions that work in other versions are preferred - to make the answer as useful as possible. Thanks in advance / Culme

UPDATE: I learned a lot from the comments and responses I received. Apart from what is stated below, I also decided to try and keep unique and identifiable controller method names to make it easier to find where they are used. As an example, instead of "Index", I'll try to use "Fidgets_IndexController" or something similar. Thus, a simple text search will go a long way in determining the calling code.

+4


source to share


3 answers


One option is Resharper - it can more or less determine where you are using Html.ActionLink()

or Html.BeginForm()

which points to the action of the controller. But it doesn't detect any messages / redirects made with JS.

Another thing you can do is use T4MVC to make links from views statically typed so you can search for links.



A text search by solution can help, but not always well, as you've already discovered - how many links per line Index

do you have in an average MVC project? So it will help with the distinguished names of the controllers / actions, but not the generic names.

Besides, you are on your own. If you try to do something smart like in JS concatenation strings to give you the correct endpoint - you're in trouble.

+2


source


The controller methods do not directly reference any part of the code (therefore 0 references), but they are dynamically called based on the RouteTable, which maps the controller methods on startup RouteConfig.RegisterRoutes(RouteTable.Routes);

to global.asax "implicitly" which maps them as / controller_name / action_name, or they can be changed by editing RouteConfig.RegisterRoutes(RouteCollection routes)

or using attributes:

[Route("myAction")]
public ActionResult MyAction() {
...
}

      

which will bind it to / myAction (no controller name)

further reading:



MSDN - Understanding the Execution Process for MVC Applications

ASP.NET MVC 5 Application Life Cycle

ASP.NET MVC Routing Overview

+3


source


Another option if you know the app well enough. Set a breakpoint on a controller action and then run some tests of your application.

0


source







All Articles