C# Response.Write() 和Response.Output.Write() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1794792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:54:02  来源:igfitidea点击:

What’s the difference between Response.Write() andResponse.Output.Write()?

c#asp.net

提问by Surya sasidhar

Possible Duplicate:
What's the difference between Response.Write() and Response.Output.Write()?

可能的重复:
Response.Write() 和 Response.Output.Write() 之间有什么区别?

How is it different from response.write() and response.output.write()? Please explain.

它与 response.write() 和 response.output.write() 有何不同?请解释。

采纳答案by Graviton

See this:

看到这个

The difference between Response.Write()and Response.Output.Write()in ASP.NET. The short answer is that the latter gives you String.Format-styleoutput and the former doesn't. The long answer follows.

Response.Write()Response.Output.Write()在 ASP.NET 中的区别。简短的回答是后者给你String.Format-style输出,而前者没有。长答案如下。

In ASP.NET the Responseobject is of type HttpResponseand when you say Response.Writeyou're really saying (basically) HttpContext.Current.Response.Writeand calling one of the many overloaded Writemethods of HttpResponse.

在ASP.NET的Response对象类型HttpResponse,当你说Response.Write你是真心的(基本)HttpContext.Current.Response.Write和调用的许多重载一个Write方法HttpResponse

Response.Writethen calls .Write()on it's internal TextWriterobject:

Response.Write然后调用.Write()它的内部TextWriter对象:

public void Write(object obj){ this._writer.Write(obj);} 

HttpResponsealso has a Property called Outputthat is of type, yes, TextWriter, so:

HttpResponse也有一个名为 的属性Output,其类型为,是的TextWriter,因此:

public TextWriter get_Output(){ return this._writer; } 

Which means you can do the Responsewhatever a TextWriterwill let you. Now, TextWriters support a Write()method aka String.Format, so you can do this:

这意味着您可以随心所欲地做Response任何事情TextWriter。现在,TextWriters 支持一种Write()方法 aka String.Format,因此您可以执行以下操作:

Response.Output.Write("Scott is {0} at {1:d}", "cool",DateTime.Now);

But internally, of course, this is happening:

但在内部,当然,这正在发生:

public virtual void Write(string format, params object[] arg)
{ 
this.Write(string.Format(format, arg)); 
}

回答by Andrew Hare

Nothing, they are synonymous (Response.Writeis simply a shorter way to express the act of writing to the response output).

没什么,它们是同义词(Response.Write只是表达写入响应输出的行为的一种更短的方式)。

If you are curious, the implementation of HttpResponse.Writelooks like this:

如果你好奇,它的实现HttpResponse.Write看起来像这样:

public void Write(string s)
{
    this._writer.Write(s);
}

And the implementation of HttpResponse.Outputis this:

的实现HttpResponse.Output是这样的:

public TextWriter Output
{
    get
    {
        return this._writer;
    }
}

So as you can see, Response.Writeand Response.Output.Writeare truly synonymous expressions.

因此,大家可以看到,Response.Write并且Response.Output.Write是真正的同义表达。

回答by Tzury Bar Yochay

Response.write() don't give formatted output. The latter one allows you to write formatted output.

Response.write() 不提供格式化输出。后一个允许您编写格式化输出。

Response.write - it writes the text stream Response.output.write - it writes the HTTP Output Stream.

Response.write - 它写入文本流 Response.output.write - 它写入 HTTP 输出流。

回答by Binod Kumar

Response.write()is used to display the normal text and Response.output.write()is used to display the formated text.

Response.write()用于显示普通文本,Response.output.write()用于显示格式化文本。

回答by jai

Here Response.Write():to display only string and you can not display any other data type values like int,date,etc.Conversion(from one data type to another) is not allowed. whereas Response .Output .Write(): you can display any type of data like int, date ,string etc.,by giving index values.

这里 Response.Write(): to display only string and you can not display any other data type values like int,date,etc.Conversion(from one data type to another) is not allowed. 不允许转换(从一种数据类型到另一种)。而 Response .Output .Write():您可以通过提供索引值来显示任何类型的数据,例如 int、date、string 等。

Here is example:

这是示例:

protected void Button1_Click(object sender, EventArgs e)
    {
       Response.Write ("hi good morning!"+"is it right?");//only strings are allowed        
       Response.Write("Scott is {0} at {1:d}", "cool", DateTime.Now);//this will give error(conversion is not allowed)
       Response.Output.Write("\nhi goood morning!");//works fine
       Response.Output.Write("Jai is {0} on {1:d}", "cool", DateTime.Now);//here the current date will be converted into string and displayed
    }