3

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

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

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

4

3 回答 3

6

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

Response.Write(string.Format("{0}{1}:", myString, myProp));
于 2013-03-11T15:02:59.047 回答
6

您可以编写一个方法,例如

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);
}
于 2013-03-11T15:04:55.993 回答
0

您可以使用:

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

或者为 Response.Write 创建一个扩展方法,使用字符串、object[] 作为参数。

于 2013-03-11T15:03:45.893 回答