我寫了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瀏覽器
回答
Console.WriteLine(...)
將不會顯示。如果你確實需要看到在調試輸出,你將不得不使用
System.Diagnostics.Debug.WriteLine("This will be displayed in output window");
,並查看它在輸出窗口。您可以通過給Debug -> Window -> Output
打開輸出窗口:
這裏是什麼這一切看起來像一個例子:
如需進一步閱讀,check out this SO post。
除了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這樣的東西將調試信息寫入日誌文件。
你可以到你的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並將其注入到該頁面。
在MVC5上測試它一樣,它不工作...應該做一個額外的配置? – Nacho
什麼不工作?如果我有更多信息,我可以嘗試提供幫助。 – MichaelTaylor3D
HttpContext未在此代碼段中定義 – brennanyoung
感謝+ 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);
}
}
- 1. ASP.NET MVC - 跨瀏覽
- 2. ASP.NET MVC服務器文件瀏覽器
- 3. asp.net mvc瀏覽器兼容性 - 爲哪種瀏覽器設計?
- 4. ASP.Net MVC禁用瀏覽器緩存(firefox)
- 5. asp.net WebForm或MVC瀏覽器遊戲
- 6. 突然瀏覽器關閉在asp.net mvc
- 7. asp.net的MVC - 在瀏覽
- 8. ASP.net MVC目錄瀏覽
- 9. 移動瀏覽器和桌面瀏覽器的常見ASP.NET MVC網站
- 10. 如何使用Console.WriteLine在ASP.Net MVC 3
- 11. ASP.NET中的Console.WriteLine
- 12. API控制器不能在瀏覽器中的MVC asp.net
- 13. 瀏覽器刷新asp.net 3.5
- 14. 瀏覽器唯一id asp.net
- 15. ASP.NET報表瀏覽器
- 16. 模擬瀏覽器的ASP.NET
- 17. VS2005,2008 - ASP.NET瀏覽器設置
- 18. ASP.Net瀏覽器兼容性
- 19. asp.net和鉻/ Safari瀏覽器
- 20. 瀏覽器關閉和asp.net
- 21. asp.net MVC瀏覽文件夾路徑
- 22. 無法瀏覽asp.net mvc 4頁
- 23. MVC 3和「JavaScript禁用」瀏覽器
- 24. 瀏覽器如何識別ASP.NET/C#中的用戶斷開瀏覽/瀏覽器瀏覽器#
- 25. 在瀏覽器中使用瀏覽器時,ASP.NET未加載ASP.NET網站
- 26. 在瀏覽器中使用ASP.NET MVC顯示SSRS報告頁面?
- 27. ASP.NET MVC - 瀏覽器中的View Source中可見的HttpException
- 28. 基於Web瀏覽器/設備(例如iPhone)的ASP.NET MVC路由
- 29. ASP.NET MVC DROPDOWNLIST保留在瀏覽器中選擇的值後
- 30. 查看在瀏覽器中的MVC 3 ASP.NET
如果你在調試模式輸出窗口應該顯示它嗎?沒有? – adt
@adt號'Console.WriteLine'不會顯示在輸出窗口中,因爲它是由ASP中的瀏覽器調用的。 – Sam