Response.Write()和Response.Output.Write()之间有什么区别?

Sur*_*har 18 c# asp.net

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

它与response.write()和response.output.write()的区别是有问题的,谢谢你.

Gra*_*ton 26

看到这个:

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

在ASP.NET中,Response对象是类型的HttpResponse,当你说Response.Write你真的在说(基本上)HttpContext.Current.Response.Write并调用其中一个重载Write方法时HttpResponse.

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

public void Write(object obj){ this._writer.Write(obj);} 
Run Code Online (Sandbox Code Playgroud)

HttpResponse还有一个叫做Output类型的属性,是的TextWriter,所以:

public TextWriter get_Output(){ return this._writer; } 
Run Code Online (Sandbox Code Playgroud)

这意味着你可以做Response任何TextWriter让你失望的事情.现在,TextWriters支持一种Write()方法String.Format,所以你可以这样做:

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

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

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

  • 从谷歌偶然发现 - 链接的原始内容(转载于此答案)来自Hanselman博客上的'04帖子 -​​ http://www.hanselman.com/blog/ASPNETResponseWriteAndResponseOutputWriteKnowTheDifference.aspx (2认同)

小智 9

这里Response.Write():只显示字符串,你不能显示任何其他数据类型值,如int,date等.不允许转换(从一种数据类型到另一种数据类型).而Response .Output .Write():你可以通过给出索引值来显示任何类型的数据,如int,date,string等.

这是一个例子:

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
    }
Run Code Online (Sandbox Code Playgroud)


小智 6

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