Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get syntax highlighting of controllers and actions on HtmlHelper extension method?

How can I get Visual Studio to provide controller and action syntax highlighting like it offers for its own object for controller and action strings in the following?

When you use:

Html.Action("Index", "Home");

Visual Studio provides additional highlighting on the controller and action, it even tries to detect which ones are available and will present it as an option.

Notice in the image below how it give me options for the actions based upon the controller that I've entered. This is what I would like to happen for my custom code. Controller / Action Goodness

I have perfectly working HtmlHelper extension method similar to the following:

public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, string controllerName, string myAwesomeField) {
        return Action(htmlHelper, actionName, controllerName, null /* routeValues */, myAwesomeField);
    }

My extension methods are getting picked up by intellisense in the regard that it knows I need a few strings. When you look at Microsoft's code, the parameters for action and controller are strings, but Visual Studio knows whether those controllers and actions exists and provide additional intellisense support. How does the tooling pick up on it?

like image 228
caschw Avatar asked Oct 27 '12 03:10

caschw


1 Answers

If you are using Resharper (of course you are) then here's a perfect solution that I had working in 5 minutes:

Follow the instruction here at the Jetbrains blog. I elected for option 2 of having the annotations source code in a file named jetbrains.Annnotations.cs included in my project. Then simply by adding the parameter attribute decorations as below (in my method WithLink) everything just works.

public void WithLink([AspMvcAction] string action, [AspMvcController] string controller)

Nice

like image 174
Simon Sanderson Avatar answered Sep 28 '22 08:09

Simon Sanderson