Providing Intellisense, Navigation and more for Custom Helpers in ASP.NET MVC
December 7th, 2011 by Hadi HaririYou 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:
Navigation
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.
Using JetBrains.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.
Tags: ReSharper

December 7th, 2011 at 4:48 pm
What about for RouteNames?
December 7th, 2011 at 4:55 pm
Also, where can I download it the source for these annotations?
December 7th, 2011 at 6:00 pm
@Daniel,
What is it you need to do with RouteNames? The source code for the annotations, as highlighted in the post is actually available via ReSharper | Options. See “Referencing the annotations” section.
December 7th, 2011 at 6:30 pm
Just the other day I wrote an answer in StackOverflow regarding this. I’ll have to update it! http://stackoverflow.com/questions/8371103/magic-strings-in-asp-net-mvc/8371241#8371241
December 7th, 2011 at 11:20 pm
Is there a facility to support custom view search paths?
December 8th, 2011 at 12:11 am
@Matt, if you use standard view engine and altering ViewLocationFormats or other location properties - it’s already there!
ReSharper detects that and use new location formats.
December 8th, 2011 at 2:07 pm
How can I disable razor processing? It takes ages to process all views and r# reprocesses them on solution open.
December 8th, 2011 at 5:56 pm
@Slava - we build tooling for other dev teams in our company, and we do something like this in the base application class:
foreach (IViewEngine x in ViewEngines.Engines) {
var wbe = x as WebFormViewEngine;
if (wbe != null) {
// append to wbe.MasterLocationFormats
// append to wbe.viewLocationFormats
// etc
}
var rve = x as RazorViewEngine;
if (rve != null) {
// apend to rve.masterLocationFormats
// append to rve.viewlocationformats
//etc
}
}
Is there a way to use annotations or build a descriptor that resharper can consume from a run time view engine?
December 8th, 2011 at 9:50 pm
Matt, there is no such descriptor currently.
Altrough, ReSharper should detect these modifications as you provided, but only in source code.
If you modify them in base application and reference that application binary - R# will miss that.
December 9th, 2011 at 1:15 am
@derigel Thanks, I will take another look!
December 13th, 2011 at 2:05 pm
Is there a way to use this in object properties? I he a model that represents my menu and some of its properties are Action, Controller and Area. If I could decorate this properties with these annotations it would be great.
December 14th, 2011 at 7:20 pm
@Carles, nope, currently these attributes are only recognized on arguments or methods.