我System.Console.WriteLine("How can I see this debugging information in a browser");在我的 ASP.NET MVC4 项目的模型中编写。如何在浏览器控制台或至少在 Visual Studio 中看到此调试字符串?我在 Visual Studio 的输出窗口中找不到它。也许我需要从 NuGet 安装一些插件?
			
			93844 次
		
5 回答
            48        
        
		
Console.WriteLine(...)不会显示。如果您绝对需要在调试器中查看输出,则必须使用
System.Diagnostics.Debug.WriteLine("This will be displayed in output window");
并在输出窗口中查看。您可以通过以下方式打开输出窗口Debug -> Window -> Output:

下面是一个例子:

如需进一步阅读,请查看此 SO 帖子。
于 2013-02-05T18:00:43.277   回答
    
    
            24        
        
		
您可以使用以下类从 C# 代码写入 Javascript 控制台
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 并将其注入到页面中。
于 2014-06-09T20:01:28.990   回答
    
    
            6        
        
		
您可以使用Debug.Writeline("debug information"). 它将显示在“输出”窗口中。
于 2013-02-05T17:57:40.613   回答
    
    
            4        
        
		
除了 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之类的东西将调试信息写入日志文件。
于 2013-02-05T18:50:56.907   回答
    
    
            1        
        
		
感谢 +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);
        }
    }
于 2015-11-19T07:54:27.213   回答