如何在WebMatrix中调试和/或跟踪执行流程?

时间:2011-02-25 02:08:57

标签: c# webmatrix

我刚刚进入Web开发(从Windows应用程序开发背景),WebMatrix似乎是一个很好的起点,因为它简单,而且因为它看起来像是一个有用的基础ASP.NET MVC发展。

然而,缺乏调试工具会造成一些伤害,特别是在尝试学习Web环境中的开发基础时。

跟踪执行流程并在页面上显示跟踪数据,似乎是一个相当基本的功能,可以获得绝对最小的调试体验,但即使这样也不会内置到WebMatrix中(或者我可能没有发现它。)

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

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

3 个答案:

答案 0 :(得分:5)

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 Express ASP.NET MVC3 RTM 。这将为您提供WebMatrix中方便的“启动Visual Studio”按钮。别担心因为你还在制作Razor网页网站,它恰好在Visual Studio中。

答案 1 :(得分:4)

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

答案 2 :(得分:1)

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>
 }
相关问题