2013-02-05 207 views
4

我寫了System.Console.WriteLine("How can I see this debuggin information in a browser");裏面模型的asp.net mvc4項目。我如何在瀏覽器或至少在visual studio中看到這個調試字符串。在visual studio的輸出窗口中找不到它。也許我需要安裝一些插件nuget ...Asp.net mvc Console.WriteLine瀏覽器

+1

如果你在調試模式輸出窗口應該顯示它嗎?沒有? – adt

+0

@adt號'Console.WriteLine'不會顯示在輸出窗口中,因爲它是由ASP中的瀏覽器調用的。 – Sam

回答

26

Console.WriteLine(...)將不會顯示。如果你確實需要看到在調試輸出,你將不得不使用

System.Diagnostics.Debug.WriteLine("This will be displayed in output window"); 

,並查看它在輸出窗口。您可以通過給Debug -> Window -> Output打開輸出窗口:

enter image description here

這裏是什麼這一切看起來像一個例子:

enter image description here

如需進一步閱讀,check out this SO post

0

您可以使用Debug.Writeline("debug information")。它將顯示在輸出窗口中。

+0

這與被接受的答案 – Burgi

3

除了Sam的回答,您可能會發現Response.Write有用。在某些情況下 - 例如,當你在支持傳統的內聯.aspx頁 - 這是更方便的寫出嫌疑值到瀏覽器來調試:

String myString = GetAStringFromSomewhere(); 

/* What did that method actually return, anyway? 
    NB: Remove this once I know! */ 
Response.Write(myString); 

這是在ASP.Net MVC不太實用,但是,因爲你的控制器將被編譯。在這種情況下,您最好使用類似log4net這樣的東西將調試信息寫入日誌文件。

9

你可以到你的Javascript控制檯從C#代碼中使用下面的類

using System.Web; 

public static class Javascript 
{ 
    static string scriptTag = "<script type=\"\" language=\"\">{0}</script>"; 
    public static void ConsoleLog(string message) 
    {  
     string function = "console.log('{0}');"; 
     string log = string.Format(GenerateCodeFromFunction(function), message); 
     HttpContext.Current.Response.Write(log); 
    } 

    public static void Alert(string message) 
    { 
     string function = "alert('{0}');"; 
     string log = string.Format(GenerateCodeFromFunction(function), message); 
     HttpContext.Current.Response.Write(log); 
    } 

    static string GenerateCodeFromFunction(string function) 
    { 
     return string.Format(scriptTag, function); 
    } 
} 

工程就像它的JS版本寫的,它實際上你的郵件轉換爲JS並將其注入到該頁面。

+0

在MVC5上測試它一樣,它不工作...應該做一個額外的配置? – Nacho

+0

什麼不工作?如果我有更多信息,我可以嘗試提供幫助。 – MichaelTaylor3D

+1

HttpContext未在此代碼段中定義 – brennanyoung

0

感謝+ MichaelTaylor3D的解決方案,我進一步增強了它的一點點,以支持ajax部分回發期間的功能。請記住添加對System.Web.Extension的引用以支持ScriptManager。

public static class Javascript 
    { 
     static string scriptTag = "<script type=\"\" language=\"\">{0}</script>"; 
     public static void ConsoleLog(string message) 
     { 
      string function = "console.log('{0}');"; 
      string log = string.Format(GenerateCodeFromFunction(function), message); 

      Page page = HttpContext.Current.Handler as Page; 

      if (ScriptManager.GetCurrent(page).IsInAsyncPostBack) 
      { 
       ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "log", "console.log('" + message + "')", true); 
      } 
      else 
      { 
       HttpContext.Current.Response.Write(log); 
      } 
     } 

     public static void ConsoleError(string message) 
     { 
      string function = "console.error('{0}');"; 
      string log = string.Format(GenerateCodeFromFunction(function), message); 

      Page page = HttpContext.Current.Handler as Page; 

      if (ScriptManager.GetCurrent(page).IsInAsyncPostBack) 
      { 
       ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "error", "console.error('" + message + "')", true); 
      } 
      else 
      { 
       HttpContext.Current.Response.Write(log); 
      } 
     } 

     public static void Alert(string message) 
     { 
      string function = "alert('{0}');"; 
      string log = string.Format(GenerateCodeFromFunction(function), message); 

      Page page = HttpContext.Current.Handler as Page; 

      if (ScriptManager.GetCurrent(page).IsInAsyncPostBack) 
      { 
       ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "alert", "alert('" + message + "')", true); 
      } 
      else 
      { 
       HttpContext.Current.Response.Write(log); 
      } 
     } 

     static string GenerateCodeFromFunction(string function) 
     { 
      return string.Format(scriptTag, function); 
     } 
    }