asp.net-mvcvisual-studiovisual-studio-extensionsdesigntime-attributes

Mark string parameters as MVC controller or view names in Visual Studio


Is there any way to add design time metadata to Visual Studio that would suggest a string parameter should correspond to the name of a controller or view? MVC already has this type of intellisense when authoring

I have some custom Html Helper Extensions that take in Controllers and Views by name and would love the syntax highlighting / autocomplete support that is available to native methods like Controller.View or Url.Action. Here's a trivial example:

public static string IsActiveClass(this HtmlHelper html, string action, string controller)
{
    return IsActiveBool(html, action, controller) ? "active" : "";
}

And here's a side by side of the native intellisense compared to custom methods which are just treated like a string:

AutoComplete Screenshot

Obviously, this could be checked at runtime via reflection, but I'd like to see if there's anyway to identify it at design time. Another answerable question might be how is Visual Studio currently performing this (especially as a way to identify if it's possible to tap into that process)


Solution

  • All credit to tocqueville... You can use JetBrains.Annotations to Provide Intellisense, Navigation and more for Custom Helpers in ASP.NET MVC:

    1. Include the JetBrains.Annotations.dll in your project and bin
    2. Use the following parameter attributes in your methods:

      • AspMvcView - indicates the parameter is a View
      • AspMvcAction - indicates the parameter is an Action
      • AspMvcController - indicates the parameter is a Controller

      public static string IsActiveClass(this HtmlHelper html,
          [AspMvcAction]string action,
          [AspMvcController]string controller)
      {
          return IsActiveBool(html, action, controller) ? "active" : "";
      }
      

    Which resharper will consume in it's static code analysis:

    Controller Action View Intellisense