ASP.NET,Response.Write和Response.Output.Write-知道区别

A fellow emailed me just now wanting to know the difference between Response.Write() and Response.Output.Write() in ASP.NET.  Well sir, I'm glad you asked, because it's damned interesting. :)  The short answer is that the latter gives you String.Format-style output and the former doesn't.  The long answer follows.

刚才有一个人给我发了电子邮件,想知道ASP.NET中Response.Write()Response.Output.Write()之间的区别。 好吧先生,我很高兴你问,因为这太有趣了。 :)简短的答案是,后者为您提供String.Format样式的输出,而前者则没有。 长答案如下。

In ASP.NET the Response object is of type HttpResponse and when you say Response.Write you're really saying (basically) HttpContext.Current.Response.Write and calling one of the many overloaded Write methods of HttpResponse. 

在ASP.NET中,Response对象的类型为HttpResponse,当您说Response.Write时,您实际上是在说(基本上)是HttpContext.Current.Response.Write并调用HttpResponse的许多重载Write方法之一。

Response.Write then calls .Write() on it's internal TextWriter object:

然后,Response.Write对其内部TextWriter对象调用.Write():

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

公共无效Write(object obj){this._writer.Write(obj);}

HttpResponse also has a Property called Output that is of type, yes, TextWriter, so:

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

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

公共TextWriter get_Output(){返回this._writer; }

Which means you can to the Response whatever a TextWriter will let you.  Now, TextWriters support a Write() method ala String.Format, so you can do this:

这意味着无论TextWriter允许您执行什么操作,您都可以响应。 现在,TextWriters支持Write()方法ala String.Format,因此您可以执行以下操作:

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

Response.Output.Write(“ Scott在{1:d}为{0}”,“ cool”,DateTime.Now);

But internally, of course, this this is happening:

但是,在内部,这确实在发生:

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

公共虚拟无效Write(字符串格式,params对象[] arg) { this.Write(string.Format(format,arg)); }

翻译自: https://www.hanselman.com/blog/aspnet-responsewrite-and-responseoutputwrite-know-the-difference

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值