GGH-Team
GGH-Team

Reputation: 19

Recognize JavaScript calls to C# functions

At my job, my team and I work on an .NET MVC project in Visual Studio where we often have to call controller or model functions from within JavaScript files. For example:

$.ajax({
                "dataType": 'json',
                "type": "POST",
                "url": "/Lookup/CreateLeadForEmployee"

This is used to call the CreateLeadForEmployee method from within the lookup controller. The problem is that whenever we want to rename these kinds of functions or find references to them, we have to use a much more manual search process instead of Visual Studio's built in capabilities (or those of different extensions and tools). This often leads to errors and issues, as it's easy to miss certain references.

What I'm wondering is if there is some kind of tool, package, add-on, or code syntax that would allow me and my team to tell Visual Studio to recognize things like the example I provided as invocations of specific C# functions? To put it another way, how do I make it so that when I right-click on CreateLeadForEmployee in Visual Studio and select "Find all references", it will include any references to the function made from a JavaScript file?

Upvotes: 1

Views: 72

Answers (2)

Shyju
Shyju

Reputation: 218852

One thing you can do is to use the Url.Action helper method to generate the relative path to this action method.

So instead of doing url: "/Lookup/CreateLeadForEmployee", You may do

url: "@Url.Action("CreateLeadForEmployee","Lookup")"

When razor executes the page, it will execute the Url.Action method and generate the relative url to this action method.

The above will work if you are using the javascript code inside a razor view, If you are using the url's inside an external js file, you should generate and set the relative url values to a variable and use it in your external js file as explained in this post.

With this approach visual studio's "Find Usages" will spot this location.

Upvotes: 1

Oluwafemi
Oluwafemi

Reputation: 14899

The quick answer to your question is, there is no tool for this.

The Server-side intellisense focuses on only the server-side components whilst the client-side intellisense deals with only client-side components for example scripts, css, html elements and attributes.

An alternative is to search using crtl + shift + F in Visual Studio to find all occurrences of specific keywords and you can update each file displayed the on Find Results window appropriately.

Upvotes: 1

Related Questions