【问题标题】:How to show Console.WriteLine output in my browser console or output window?如何在我的浏览器控制台或输出窗口中显示 Console.WriteLine 输出?
【发布时间】:2013-02-05 17:53:41
【问题描述】:

我在我的 ASP.NET MVC4 项目的模型中写了System.Console.WriteLine("How can I see this debugging information in a browser");。如何在浏览器控制台或至少在 Visual Studio 中看到此调试字符串?我在 Visual Studio 的输出窗口中找不到它。也许我需要从 NuGet 安装一些插件?

【问题讨论】:

  • 如果你处于调试模式输出窗口应该显示吗?没有?
  • @adt No. Console.WriteLine 不会显示在输出窗口中,因为它是由 ASP 中的浏览器调用的。

标签: asp.net asp.net-mvc


【解决方案1】:

Console.WriteLine(...) 不会显示。如果您绝对需要在调试器中查看输出,则必须使用

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

并在输出窗口中查看。你可以通过Debug -> Window -> Output打开输出窗口:

下面是一个例子:

如需进一步阅读,请check out this SO post

【讨论】:

    【解决方案2】:

    您可以使用以下类从您的 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 并将其注入到页面中。

    【讨论】:

    • 在 MVC5 上测试过,它不起作用...应该做一个额外的配置吗?
    • 什么不起作用?如果我有更多信息,我可以尝试提供帮助。
    • HttpContext 没有在这个 sn-p 中定义
    • 没错,您需要确保已定义 System.Web 命名空间。我会把它添加到我的答案中。
    • 我一直收到“常量换行”错误,直到我将 scriptTag 声明更改为 static string scriptTag = "&lt;script type=\"\" language=\"\"&gt;{0}&lt;/" + "script&gt;";
    【解决方案3】:

    您可以使用Debug.Writeline("debug information")。它将显示在“输出”窗口中。

    【讨论】:

    • @Burgi 它也是在接受的答案之前编写和发布的。
    【解决方案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 之类的方式将调试信息写入日志文件。

    【讨论】:

      【解决方案5】:

      感谢 +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);
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2011-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-12
        • 2017-08-11
        相关资源
        最近更新 更多