2011-02-25 59 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> 
}