在 Visual Studio 中将字符串参数标记为 MVC 控制器或视图名称

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

有没有什么方法可以将设计时元数据添加到 Visual Studio,建议字符串参数应对应于控制器或视图的名称? MVC 在创作时已经具有这种类型的智能感知

我有一些自定义的 Html 帮助程序扩展,它们按名称接受控制器和视图,并且喜欢 Controller.View or Url.Action 等本机方法可用的语法突出显示/自动完成支持。这是一个简单的例子:

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

这里是本机智能感知与自定义方法的对比,自定义方法只是像字符串一样处理:

显然,这可以在运行时通过反射进行检查,但我想看看是否有办法在设计时识别它。另一个可以回答的问题可能是 Visual Studio 目前如何执行此操作(特别是作为一种确定是否可以利用该过程的方法)

全部归功于... You can use JetBrains.Annotations to Provide Intellisense, Navigation and more for Custom Helpers in ASP.NET MVC

  1. 在您的项目和 bin 中包含 JetBrains.Annotations.dll
  2. 在您的方法中使用以下参数属性:

    • AspMvcView - 表示参数是一个View
    • AspMvcAction - 表示参数是一个 Action
    • AspMvcController - 表示参数是控制器

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

哪个resharper会在它的静态代码分析中消耗: