Providing Intellisense, Navigation and more for Custom Helpers in ASP.NET MVC
You probably are aware by now that as of ReSharper 5, we added first-class support for ASP.NET MVC. This included among many things, the ability to provide Intellisense, Create from usage and Navigation to built-in methods such as Controller.View or Html.ActionLink:
Ctrl+Left Mouse Click or F12 will navigate to the corresponding View
or to the Action and/or Controller
Intellisense and Create From Usage
Ability to have Intellisense when providing Actions/Controllers
as well as the possibility of creating from usage
However, what happens when you want to use a custom function, for instance, a better ActionLink or your own View method? Did you know that you can still get all these goodies? All you need to do is use some Annotations.
ReSharper uses annotations via the form of .NET attributes to figure out what an ASP.NET MVC View, Action or Controller is. As such, all we need to do for our custom method and extensions to leverage this, is tell ReSharper what parameter corresponds to what.
Referencing the annotations
To use ReSharper annotations, we have mainly two options (with a third one hopefully coming soon):
1. We can include the library JetBrains.Annotations.dll in our project and reference it.
2. We can copy the annotations and include it as source in our project
[3. We can use nuget install-package JetBrains.Annotations] Coming soon!
The first option is pretty simple. The DLL is located in the ReSharper installation bin folder. For the second option, we open up ReSharper | Options and select Code Annotations entry
select the Copy default implementation to clipboard button and paste into an empty file.
Annotating custom methods
Once we’ve completed this step, all we need to do is annotate our parameters with the correct attributes. We’re interested in 3 different attributes in particular:
- AspMvcView which indicates the parameter is a View
- AspMvcAction which indicates the parameter is an Action
- AspMvcController which indicates the parameter is a Controller
Here is the header corresponding to a base controller with a custom method named ExtendedView
and here’s the header for a custom ActionLink
(the body of both methods are omitted and are not necessary to demonstrate the functionality)
As soon as we do this, ReSharper picks up these methods and offers us the same functionality that is provided for the methods that ship out of the box:
Notice how we still get Navigation (the underlining), Intellisense and Create from usage in our TheOnlyActionLink custom method. Its much the same for the ExtendedView method
That’s all there is to it.
Subscribe to Blog updates
Thanks, we've got you!
Another Look into the Future with Rider’s Predictive Debugger
In the 2023.2 release cycle, we’ve introduced the Predictive Debugger in ReSharper, which gives you predictions about code paths and variables beyond the current execution pointer. We’ve written extensively about its advantages compared to alternative debugging strategies like thorough thinking, log…
Visualize Entity Framework Relationships and Additional Query Analysis in ReSharper 2023.3
A lot of teams are using Entity Framework or EF Core to work with their database. As an Object-Relational Mapper (ORM), it bridges objects in code to a relational database model, so that as a developer you don’t have to worry too much about the actual database. We all know: that’s not entirely tr…
Automatically Analyze ASP.NET Core Performance With Dynamic Program Analysis
Slow web pages may make your users or customers abandon your web application, even before they’ve had a proper look at it. You’ve likely also been frustrated working with a web application that is slow to load. The good news is that the latest versions of ReSharper and JetBrains Rider’s Dynamic P…
OSS Power-Ups: MassTransit – Webinar Recording
The recording of our webinar, OSS Power-Ups: MassTransit, with Chris Patterson, is available. This was the thirteenth episode of our OSS Power-Ups series, where we put a spotlight on open-source .NET projects. Subscribe to our community newsletter to receive notifications about future webinars.…