2011-02-25 62 views
8

我刚刚进入Web开发(从Windows应用程序开发背景),WebMatrix似乎是一个很好的开始,因为它非常简单,而且它看起来像是实现完整的ASP.NET MVC开发的有用垫脚石。如何在WebMatrix中调试和/或跟踪执行流程?

但是,缺乏调试工具会有一点伤害,特别是在尝试学习Web环境中开发的基础知识时。

跟踪执行流程,并在页面上显示跟踪数据,似乎是一个绝对最基本的调试体验的基本功能,但即使这似乎并没有内置到WebMatrix中(或者我可能只是“吨发现它呢)。

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

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

回答

5

WebMatrix简单的一部分(以及一些它的吸引力)是缺少膨胀,如调试器和跟踪工具!话虽如此,我不会对未来版本中出现的调试器(与Intellisense一起)进行赌注。

在WebMatrix中,我们有基本的'打印变量到页面'cababilities,其中ServerInfoObjectInfo对象帮助将原始信息转储到前端。在asp.net网站上可以找到使用这些对象的快速教程:Introduction to Debugging.

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

同样有一个很好的介绍做这在asp.net网站:Program ASP.NET Web Pages in Visual Studio.

的关键点安装的Visual Web Developer 2010速成ASP.NET MVC3 RTM。这也会给你一个方便的WebMatrix中的'启动Visual Studio'按钮。别担心,因为您仍在制作Razor网页网站,它恰好在Visual Studio中。

4

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

+0

考虑到环境,似乎有点更好的答案。 – VoidKing 2013-06-14 20:21:06

1

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

建立一个叫做Debug.cs文件放在App_Code目录下面的代码:

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)); 
    } 
} 

这会给你一个名为调试静态类,它具有典型的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> 
}