Visual Studio中View页面与Js页面用快捷键互相跳转

现在已经将源码放到GitHub中了

地址是

https://github.com/liningit/ViewJsLN

公司开发的项目使用的是Mvc框架,且Js与View页面是分开在两个文件夹下的,所以如果在View页面转到Js页面或者反过来都要先找到页面打开,很不方便.于是我做了一个VS外接程序可以很方便的用快捷键就直接互相跳转.

公司网站目录是如下图所示:View目录有Js分别在两个文件夹下:Views与Scripts

  1. 先打开VS新建项目:VS外接程序
  2. 因为我是用C#开发的所以选择Using C#
  3. 然后在下面这个图选中创建Tool这个选项
  4. 打开Connect.cs文件修改OnConnection中的一行
       Command command = commands.AddNamedCommand2(_addInInstance, "MyAddinJSView", "JSView互跳", "JSView互跳", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
  5. 修改Exec方法如下
    复制代码
      public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
            {
                handled = false;
                if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
                {
                    if (commandName == "MyAddinJSView.Connect.MyAddinJSView")
                    {
                        handled = true;
                        try
                        {
                            var file = _applicationObject.ActiveDocument;
                            string opFile = string.Empty;
                            if (file.FullName.ToLower().EndsWith(".cshtml"))
                            {
                                opFile = file.FullName.ToLower().Replace(@"\views\", @"\scripts\").Replace(".cshtml", ".js");
                            }
                            else if (file.FullName.ToLower().EndsWith(".js"))
                            {
                                opFile = file.FullName.ToLower().Replace(@"\scripts\", @"\views\").Replace(".js", ".cshtml");
                            }
                            if (!string.IsNullOrEmpty(opFile))
                            {
                                _applicationObject.ItemOperations.OpenFile(opFile);
                            }
                        }
                        catch (Exception)
                        { 
                        }
                        return;
                    }
                }
            }
    复制代码
  6. 然后生成dll把MyAddinJSView.dll考到一个目录下
  7. 打开MyAddinJSView - For Testing.AddIn文件所在的目录,我的是在
    C:\Users\Administrator\Documents\Visual Studio 2013\Addins下
  8. 修改 <Assembly>D:\LN\项目\MyAddinJSView\MyAddinJSView\bin\MyAddinJSView.dll</Assembly>MyAddinJSView.dll的目录
  9. 这时候在打开vs在工具下面又一个js互跳的菜单了,如果没有则在工具>外接程序里面启用一下试试看
  10. 然后打开工具>自定义,点击下面的键盘按钮
  11. 在显示命令包含里面打myadd搜到我们新增的菜单设个快捷键并点击分配按钮就可以了

 

posted @   liningit  阅读(3145)  评论(2编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示