Console.WriteLine到Response.Write?

mar*_*zzz 2 .net c# webforms

我在WebForm中,我想使用这种格式的通用Console.Writeline:

Console.WriteLine("{0}{1}:", myString, myProp);
Run Code Online (Sandbox Code Playgroud)

使用Response.Write(将代码放在客户端).我可以吗?

PaR*_*RaJ 6

你可以写一个类似的方法

public static void WriteToResponse(string format, params object[] args)
{
    Response.Write(String.Format(format, args));
}
Run Code Online (Sandbox Code Playgroud)

上面的方法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);
}
Run Code Online (Sandbox Code Playgroud)


Oma*_*mar 5

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

Response.Write(string.Format("{0}{1}:", myString, myProp));
Run Code Online (Sandbox Code Playgroud)