欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

HttpResponse的Output與OutputStream、Filter關(guān)系與區(qū)別介紹

 更新時(shí)間:2012年11月07日 15:13:27   作者:  
在網(wǎng)上經(jīng)??匆娪羞@樣的代碼HttpResponse response = HttpContext.Current.Response;現(xiàn)在我也來說說這幾個(gè)東東是什么吧
在網(wǎng)上經(jīng)??匆娪羞@樣的代碼

HttpResponse response = HttpContext.Current.Response;
response.Filter = new PageFilter(response.Filter);

來攔截輸出流,自己也做個(gè)類似的東東,如asp.net中 js 合并 壓縮,現(xiàn)在我也來說說這幾個(gè)東東是什么吧,需要大家對asp.net的生命周期比較熟悉,如不熟悉的朋友建議先看看ASP.NET 請求處理流程 ASP.NET管線與應(yīng)用程序生命周期

首先我們來看看這3個(gè)屬性的源代碼吧:

復(fù)制代碼 代碼如下:

public TextWriter Output
{
get
{
return this._writer;
}
set
{
this._writer = value;
}
}


public Stream OutputStream
{
get
{
if (!this.UsingHttpWriter)
{
throw new HttpException(SR.GetString("OutputStream_NotAvail"));
}
return this._httpWriter.OutputStream;
}
}


復(fù)制代碼 代碼如下:

public Stream Filter
{
get
{
if (this.UsingHttpWriter)
{
return this._httpWriter.GetCurrentFilter();
}
return null;
}
set
{
if (!this.UsingHttpWriter)
{
throw new HttpException(SR.GetString("Filtering_not_allowed"));
}
this._httpWriter.InstallFilter(value);
IIS7WorkerRequest request = this._wr as IIS7WorkerRequest;
if (request != null)
{
request.ResponseFilterInstalled();
}
}
}

我們看到Filter和OutputStream都用到了一個(gè)屬性UsingHttpWriter,那這個(gè)屬性是怎么定義的了
復(fù)制代碼 代碼如下:

private bool UsingHttpWriter
{
get
{
return ((this._httpWriter != null) && (this._writer == this._httpWriter));
}
}

從這個(gè)屬性我們可以知道_writer 、_httpWriter實(shí)際上是同一個(gè)東東,它們的類型是HttpWriter ,而HttpWriter 又繼承與TextWriter。現(xiàn)在我們可以解釋Output就是_httpWriter,而OutputStream是_httpWriter的OutputStream屬性。類HttpWriter 主要代碼如下
復(fù)制代碼 代碼如下:

public Stream OutputStream
{
get
{
return this._stream;
}
}


internal HttpWriter(HttpResponse response) : base(null)
{
this._response = response;
this._stream = new HttpResponseStream(this);
this._buffers = new ArrayList();
this._lastBuffer = null;
this._charBuffer = (char[]) s_Allocator.GetBuffer();
this._charBufferLength = this._charBuffer.Length;
this._charBufferFree = this._charBufferLength;
this.UpdateResponseBuffering();
}


internal HttpResponseStream(HttpWriter writer)
{
this._writer = writer;
}

HttpResponse 在Filter屬性設(shè)置調(diào)用了HttpWriter類的InstallFilter方法,而獲取調(diào)用了該類的GetCurrentFilter
復(fù)制代碼 代碼如下:

internal void InstallFilter(Stream filter)
{
if (this._filterSink == null)
{
throw new HttpException(SR.GetString("Invalid_response_filter"));
}
this._installedFilter = filter;
}

internal Stream GetCurrentFilter()
{
if (this._installedFilter != null)
{
return this._installedFilter;
}
if (this._filterSink == null)
{
this._filterSink = new HttpResponseStreamFilterSink(this);
}
return this._filterSink;
}

由以上代碼我們可以得知HttpResponse的輸出流就是Filter屬性設(shè)置的流,即HttpResponse的Output和OutputStream屬性的輸出流都是來自Filter中的流。我們來看看_writer 、_httpWriter它們是在什么時(shí)候初始化的了?在HttpResonse中有一個(gè)方法
復(fù)制代碼 代碼如下:

internal void InitResponseWriter()
{
if (this._httpWriter == null)
{
this._httpWriter = new HttpWriter(this);
this._writer = this._httpWriter;
}
}

該方法是由HttpRuntime的ProcessRequestInternal來調(diào)用
復(fù)制代碼 代碼如下:

private void ProcessRequestInternal(HttpWorkerRequest wr)
{
HttpContext context;
try
{
context = new HttpContext(wr, false);
}
catch
{
wr.SendStatus(400, "Bad Request");
wr.SendKnownResponseHeader(12, "text/html; charset=utf-8");
byte[] bytes = Encoding.ASCII.GetBytes("<html><body>Bad Request</body></html>");
wr.SendResponseFromMemory(bytes, bytes.Length);
wr.FlushResponse(true);
wr.EndOfRequest();
return;
}
wr.SetEndOfSendNotification(this._asyncEndOfSendCallback, context);
Interlocked.Increment(ref this._activeRequestCount);
HostingEnvironment.IncrementBusyCount();
try
{
try
{
this.EnsureFirstRequestInit(context);
}
catch
{
if (!context.Request.IsDebuggingRequest)
{
throw;
}
}
context.Response.InitResponseWriter();
IHttpHandler applicationInstance = HttpApplicationFactory.GetApplicationInstance(context);
if (applicationInstance == null)
{
throw new HttpException(SR.GetString("Unable_create_app_object"));
}
if (EtwTrace.IsTraceEnabled(5, 1))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_START_HANDLER, context.WorkerRequest, applicationInstance.GetType().FullName, "Start");
}
if (applicationInstance is IHttpAsyncHandler)
{
IHttpAsyncHandler handler2 = (IHttpAsyncHandler) applicationInstance;
context.AsyncAppHandler = handler2;
handler2.BeginProcessRequest(context, this._handlerCompletionCallback, context);
}
else
{
applicationInstance.ProcessRequest(context);
this.FinishRequest(context.WorkerRequest, context, null);
}
}
catch (Exception exception)
{
context.Response.InitResponseWriter();
this.FinishRequest(wr, context, exception);
}
}

相關(guān)文章

  • .NET 6 即將到來的新特性  隱式命名空間引用

    .NET 6 即將到來的新特性 隱式命名空間引用

    ASP.NET 現(xiàn)在我們還是需要手動加命名空間引用,在以后的版本中可能就不需要手動加命名空間的引用了,本文就來介紹.NET 6即將到來的新特性--隱式命名空間引用,,需要的朋友可以參考下面文章內(nèi)容
    2021-09-09
  • 用CSS實(shí)現(xiàn)圖片傾斜 只支持IE

    用CSS實(shí)現(xiàn)圖片傾斜 只支持IE

    用CSS實(shí)現(xiàn)圖片傾斜 只支持IE...
    2007-11-11
  • .Net性能調(diào)優(yōu)-ArrayPool詳情

    .Net性能調(diào)優(yōu)-ArrayPool詳情

    ArrayPool具有高性能 托管 數(shù)組緩沖池,可重復(fù)使用,用 租用 空間的方式代替 重新分配 數(shù)組空間的行為的特點(diǎn)及可以在頻繁創(chuàng)建和銷毀數(shù)組的情況下 提高性能 ,減少垃圾回收器的壓力的優(yōu)點(diǎn),下面文章內(nèi)容將詳細(xì)對其做介紹,需要的朋友可以參考一下
    2021-09-09
  • 在NET?Core?中獲取?CPU?使用率

    在NET?Core?中獲取?CPU?使用率

    這篇文章我們分享一種如何在?.NETCore?中獲取?CPU使用率的方法,?它所報(bào)告的這個(gè)值和?任務(wù)管理器?中報(bào)告的?CPU?使用值?差不多是一致的,下面來看看文中的具體詳細(xì)介紹吧
    2022-01-01
  • ASP.NET學(xué)習(xí)中常見錯誤總結(jié)歸納

    ASP.NET學(xué)習(xí)中常見錯誤總結(jié)歸納

    這篇文章主要介紹了asp.net學(xué)習(xí)過程中碰到的常見錯誤的解決方法,通讀本篇對大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下
    2021-09-09
  • ASP.NET Core MVC 過濾器(Filter)

    ASP.NET Core MVC 過濾器(Filter)

    本文小編要給大家介紹的是ASP.NET Core MVC 過濾器,ASP.NET MVC 中的過濾器允許在執(zhí)行管道中的特定階段之前或之后運(yùn)行代碼。可以對全局,也可以對每個(gè)控制器或每個(gè)操作配置過濾器,需要的朋友可以參考下面文章的具體內(nèi)容
    2021-09-09
  • .NET提取?Thread?中返回值詳情

    .NET提取?Thread?中返回值詳情

    這篇文章主要介紹了.NET提取?Thread?中返回值詳情,關(guān)于如何獲取?Thread?中的返回值,不同的版本有不同的解決方案。需要的朋友可以參考一下
    2022-01-01
  • 使用.NET6實(shí)現(xiàn)動態(tài)API

    使用.NET6實(shí)現(xiàn)動態(tài)API

    本文詳細(xì)講解了使用.NET6實(shí)現(xiàn)動態(tài)API,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • .NET6新特新?struct優(yōu)化

    .NET6新特新?struct優(yōu)化

    這篇文章主要給大家分享的是?NET6新特新?struct優(yōu)化,在.NET6中針對Struct做了一些優(yōu)化,下面我們就通過一些案例來看一下.NET6中針對Struct的優(yōu)化,需要的朋友可以參考一下,希望對大家有所幫助
    2021-11-11
  • ASP.NET?Core使用功能開關(guān)控制路由訪問操作(續(xù))

    ASP.NET?Core使用功能開關(guān)控制路由訪問操作(續(xù))

    這篇文章主要介紹了ASP.NET?Core使用功能開關(guān)控制路由訪問操作的(續(xù)),上一篇文章我們已經(jīng)介紹過一部份該相關(guān)內(nèi)容,??在本文,我們可以判斷當(dāng)前路由地址是否為調(diào)試地址,讓評估返回真,需要的小伙伴可以參考一下
    2022-02-02

最新評論