【问题标题】:How to debug and/or trace execution flow in WebMatrix?如何在 WebMatrix 中调试和/或跟踪执行流程?
【发布时间】:2011-02-25 02:08:57
【问题描述】:

我刚刚进入 Web 开发(来自 Windows 应用程序开发背景),WebMatrix 似乎是一个不错的起点,因为它很简单,而且它看起来像是通向完整 ASP.NET MVC 的有用垫脚石发展。

然而,调试工具的缺乏会带来一些伤害,尤其是在尝试学习 Web 环境中的开发基础知识时。

跟踪执行流程,并在页面上显示跟踪数据,对于绝对最低限度的调试体验来说似乎是一项相当基本的功能,但即使这样似乎也没有内置到 WebMatrix 中(或者也许我只是没有还没找到)。

在单个页面中设置跟踪变量很容易,然后在页面布局中显示该变量。但是,当我需要跨流程中的其他页面(例如布局页面、_PageStart 页面等),甚至在页面构建过程中使用的 C# 类中跟踪执行时,这有什么帮助。

WebMatrix 中是否有我尚未找到的跟踪功能?或者,有没有办法实现一个可以在整个应用程序中工作的跟踪工具,而不仅仅是在一个页面中?即使是第三方产品 ($) 也聊胜于无。

【问题讨论】:

    标签: c# webmatrix


    【解决方案1】:

    WebMatrix 的简单性(对某些人来说,它很有吸引力)的部分原因在于没有像调试器和跟踪工具这样的臃肿!话虽如此,我不会打赌未来版本中会出现调试器(以及 Intellisense)。

    在 WebMatrix 中,我们有基本的“页面打印变量”功能,带有 ServerInfoObjectInfo 对象,它们有助于将原始信息转储到前端。可以在 asp.net 站点上找到使用这些对象的快速教程:Introduction to Debugging.

    如果您想深入了解实际 IDE 级别的调试和跟踪,那么我建议您使用 Visual Studio(任何版本都可以正常工作,包括免费的 Express 版本)。

    在 asp.net 网站上再次有一个很好的介绍:Program ASP.NET Web Pages in Visual Studio.

    重点是安装Visual Web Developer 2010 ExpressASP.NET MVC3 RTM。这也会在 WebMatrix 中为您提供一个方便的“启动 Visual Studio”按钮。不用担心,因为您仍在制作 Razor Web Pages 网站,它恰好在 Visual Studio 中。

    【讨论】:

      【解决方案2】:

      有一个Razor Debugger (当前为 0.1 版)在 WebMatrix 的 Packages (Nuget) 区域中。

      【讨论】:

      • 考虑到环境,这似乎是一个更好的答案。
      【解决方案3】:

      WebMatrix 让人回想起通过警报/打印进行调试的经典日子。不理想,但它有一定的简单性和艺术性。但是,当您的代码出现问题时,有时很难了解您的变量等等。我已经用一个简单的Debug 类解决了我的大部分调试问题。

      使用以下代码在您的 App_Code 目录中创建一个名为 Debug.cs 的文件:

      using System;
      using System.Collections.Generic;
      using System.Web;
      using System.Text;
      
      public class TextWrittenEventArgs : EventArgs {
          public string Text { get; private set; }
          public TextWrittenEventArgs(string text) {
              this.Text = text;
          }
      }
      
      public class DebugMessages {
        StringBuilder _debugBuffer = new StringBuilder();
      
        public DebugMessages() {
          Debug.OnWrite += delegate(object sender, TextWrittenEventArgs e) { _debugBuffer.Append(e.Text); };
        }
      
        public override string ToString() {
          return _debugBuffer.ToString();
        }
      }
      
      public static class Debug {
        public delegate void OnWriteEventHandler(object sender, TextWrittenEventArgs e);
        public static event OnWriteEventHandler OnWrite;
      
        public static void Write(string text) {
          TextWritten(text);
        }
      
        public static void WriteLine(string text) {
          TextWritten(text + System.Environment.NewLine);
        }
      
        public static void Write(string text, params object[] args) {
          text = (args != null ? String.Format(text, args) : text);
          TextWritten(text);
        }
      
        public static void WriteLine(string text, params object[] args) {
          text = (args != null ? String.Format(text, args) : text) + System.Environment.NewLine;
          TextWritten(text);
        }
      
        private static void TextWritten(string text) {
          if (OnWrite != null) OnWrite(null, new TextWrittenEventArgs(text));
        }
      }
      

      这将为您提供一个名为 Debug 的静态类,它具有典型的 WriteLine 方法。然后,在您的 CSHTML 页面中,您可以新建 DebugMessages 对象。你可以.ToString()它来获取调试信息。

       var debugMsg = new DebugMessages();
       try {
          // code that's failing, but calls Debug.WriteLine() with key debug info
       }
       catch (Exception ex) {
         <p>@debugMsg.ToString()</p>
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-30
        • 1970-01-01
        • 2018-03-25
        • 1970-01-01
        • 1970-01-01
        • 2011-11-09
        • 1970-01-01
        相关资源
        最近更新 更多