ASP.NET WebAPI 06 HttpMessageHandler管道

來源:互聯網
上載者:User

標籤:

HttpMessageHandler管道

在Web API的中,微軟為了更好的進行架構擴充,採用的了一套管道設計----HttpMessageHander(其實WCF也有類似架構).

 

在整個管道中的頭與尾分別是HttpServer、HttpRoutingDispatcher,它們都繼承HttpMessageHandler,其中ASP.NET 為我們預留了可擴充的DelegatingHandler,下面我們就來看看幾個類之前的關係。

SendAsync為處理請求的方法。在DelegatingHandler中有一類型為HttpMessageHandler的屬性InnerHandler。它是構成管道的關鍵。因為有了這個屬性所以的DelegatingHandler都能夠串聯起來,逐步去經過管道中的每個DelegatingHandler

 

在HttpServer有兩個新添加的屬性Configuration與Dispatcher,其中Dispatcher指向管道的尾HttpRoutingDispatcher.在HttpConfiguration有一類型為Collection< DelegatingHandler>的屬性MessageHandlers,我們這個集合中添加自訂的DelegatingHandler就可以添加到整個管道中。所以我們可以看出HttpServer中基本已經定義的整個HttpMessagHandler管道。

 

下面我們定義一個可以進行請求方式Post與Put(當然這隻是個例子)

 

    public class HttpMethodChangeHandler : DelegatingHandler    {        protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)        {            var method = request.Method;            if(request.Method == HttpMethod.Post)            {                request.Method = HttpMethod.Put;            }            else if(request.Method == HttpMethod.Put)            {                request.Method = HttpMethod.Post;            }            return base.SendAsync(request, cancellationToken);        }    }

 

 

 

我們在HttpApplication.Application_Start中加添加如下語句

 

GlobalConfiguration.Configure(t => t.MessageHandlers.Add(new HttpMethodChangeHandler()));

 

 

在DemoController中定義了Put,Post方法

 

        [HttpPost]        public string Post()        {            return "這是一個Post方法";        }        [HttpPut]        public string Put()        {            return "這是一個Put方法";        }

 

 

 

 

下面是這四種測試結果:

url:http://localhost:41666/api/Demo/Put    Method:Post

結果:"這是一個Put方法"

 

url: http://localhost:41666/api/Demo/Put        Method:Put

結果:{"Message":"請求的資源不支援 http 方法"POST"。"}

 

 

url:http://localhost:41666/api/Demo/Post        Method:Put

結果:"這是一個Post方法"

 

url: http://localhost:41666/api/Demo/Post        Method:Post

結果:{"Message":"請求的資源不支援 http 方法"PUT"。"}

 

另外我們再在DemoController定義一個擷取自訂DelegatingHandler的方法GetChains

 

 

        public IEnumerable<string> GetChains()        {            IEnumerable<string> result = GetChains(GlobalConfiguration.DefaultServer);            return result;        }        private IEnumerable<string> GetChains(DelegatingHandler handler)        {            yield return handler.GetType().FullName;            var innerHander = handler.InnerHandler as DelegatingHandler;            if (innerHander != null)            {                yield return innerHander.GetType().FullName;            }        }

 

 

 

 

 

ASP.NET WebAPI 06 HttpMessageHandler管道

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.