asp.net-mvc - 在 Visual Studio 中将字符串参数标记为 MVC Controller 或 View 名称

标签 asp.net-mvc visual-studio visual-studio-extensions designtime-attributes

是否有任何方法可以将设计时元数据添加到 Visual Studio,以建议字符串参数应与 Controller 或 View 的名称相对应? MVC 在创作时已经具有这种类型的智能感知

我有一些自定义的 Html 帮助器扩展,它们按名称接收 Controller 和 View ,并且喜欢可用于 native 方法(如 Controller.View )的语法突出显示/自动完成支持。或Url.Action 。这是一个简单的例子:

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

这是 native 智能感知与仅被视为字符串的自定义方法的并排对比:

AutoComplete Screenshot

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

最佳答案

所有功劳归于tocqueville ...您可以使用 JetBrains.Annotations Provide Intellisense, Navigation and more for Custom Helpers in ASP.NET MVC :

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

    • AspMvcView - 表示参数是一个View
    • AspMvcAction - 表示参数是一个 Action
    • AspMvcController - 表示参数是 Controller

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

哪个 resharper 将在其静态代码分析中消耗:

Controller Action View Intellisense

关于asp.net-mvc - 在 Visual Studio 中将字符串参数标记为 MVC Controller 或 View 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49754332/

相关文章:

asp.net-mvc - SportStore : WebUI. WindsorControllerFactory.GetControllerInstance(System.Type:找不到合适的方法来覆盖

.net - 关于设计和构建分布式应用程序以跟踪车辆的建议

asp.net-mvc - 使用 VB.net 2008 开始 ASP.NET MVC

sql-server - 将 SQL Server Express "Advanced"功能添加到 Visual Studio 安装的 LocalDB

css - Visual Studio Bundler & Minifier - 从 CSS 中删除重要命令

visual-studio-extensions - Visual Studio 2010 插件 - 如何在错误列表窗口中添加错误

c# - ASP.NET MVC 从属性重定向

css - 是否有工具可以在 Visual Studio 中按字母顺序排列 CSS 定义?

c# - 如何在 VSPackage 中找到解决方案使用的版本控制系统

C编程递归