Console.WriteLine to Response.Write?

本文关键字:Write Response to WriteLine Console | 更新日期: 2023-09-27 18:03:16

我在WebForm中,我想使用通用控制台。用这种格式书写:

Console.WriteLine("{0}{1}:", myString, myProp);

与反应。编写(将代码放在客户端)。我可以吗?

Console.WriteLine to Response.Write?

在这种情况下使用Console.WriteLine("{0}{1}:", myString, myProp);没有意义,但是您可以使用string.Format()方法:

Response.Write(string.Format("{0}{1}:", myString, myProp));

你可以写一个像

这样的方法
public static void WriteToResponse(string format, params object[] args)
{
    Response.Write(String.Format(format, args));
}

上面的方法是Console相当于Console.Write,你可以添加一个额外的Response.Write(Environment.NewLine),使其类似于Console.WriteLine

public static void WriteLineToResponse(string format, params object[] args)
{
    Response.Write(String.Format(format, args));
    Response.Write(Environment.NewLine);
}

您可以使用:

Response.Write (string.Format("{0}{1}:", myString, myProp)); 

或者为Response创建扩展方法。使用string, object[]作为参数。