2012-10-20 24 views
6
執行訪問電流控制器

我想知道是否有可能訪問所執行的控制器(或將要執行)在DelegatingHandler的SendAsync方法?我似乎無法弄清楚如何可以訪問它,我想,這是因爲它執行控制器執行的外...在DelegatingHandler

是否可以參考呢?

回答

15

不,因爲消息處理程序在原始HttpRequestMessage或原始HttpResponseMessage上運行(在繼續的情況下)。所以真的,有沒有因爲消息處理程序「執行電流控制器」與DelegatingHandlers的概念調度請求控制器或(再次,在延續的情況下),控制器返回效應初探前後將被調用。

但是,它真的取決於你正在嘗試做的。

如果你想知道哪個控制器最終將請求路由得到,你可以手動調用,將在內部選擇控制器的機制。

public class MyHandler : DelegatingHandler 
{ 
    protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) 
    { 
     var config = GlobalConfiguration.Configuration; 
     var controllerSelector = new DefaultHttpControllerSelector(config); 

     // descriptor here will contain information about the controller to which the request will be routed. If it's null (i.e. controller not found), it will throw an exception 
     var descriptor = controllerSelector.SelectController(request); 

     // continue 
     return base.SendAsync(request, cancellationToken); 
    } 
} 
+0

謝謝,我有一個驗證例程,這將是很好能夠檢查控制器類型,看它是否具有已定義的自定義屬性,這樣我就可以避開這個驗證程序。這應該可能。 –

+0

太棒了。是的,你可以很容易地做到這一點,一旦你抓住HttpControllerDescriptor的實例 –

+0

@FilipW有沒有一個'不錯'的方式來做到這一點,而不會拋出異常?在源代碼中調用'SelectController'最終調用'this._controllerInfoCache.Value.TryGetValue(controllerName,出controllerDescriptor)''不過是_controllerInfoCache'無法訪問公開以任何方式 – wal

0

擴展@GalacticBoy的解決方案,這將是更好地使用

public class MyHandler : DelegatingHandler 
{ 
    private static IHttpControllerSelector _controllerSelector = null; 

    protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) 
    { 
     if (_controllerSelector == null) 
     { 
      var config = request.GetConfiguration(); 
      _controllerSelector = config.Services.GetService(typeof(IHttpControllerSelector)) as IHttpControllerSelector; 
     } 

     try 
     { 
      // descriptor here will contain information about the controller to which the request will be routed. If it's null (i.e. controller not found), it will throw an exception 
      var descriptor = _controllerSelector.SelectController(request); 


     } 
     catch 
     { 
      // controller not found 
     } 

     // continue 
     return base.SendAsync(request, cancellationToken); 
    } 
} 
0

取決於你用的信息可能與在執行請求後獲得的信息,請細做。例如記錄執行的控制器/操作。

using System; 
using System.Net.Http; 
using System.Threading; 
using System.Threading.Tasks; 
using System.Web; 

namespace Example 
{ 
    public class SampleHandler : DelegatingHandler 
    { 
     protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) 
     { 
      return base.SendAsync(request, cancellationToken) 
         .ContinueWith(task => 
         { 
          HttpResponseMessage response = task.Result; 

          string actionName = request.GetActionDescriptor().ActionName; 
          string controllerName = request.GetActionDescriptor().ControllerDescriptor.ControllerName; 

          // log action/controller or do something else 

          return response; 
         }, cancellationToken); 
     } 
    } 
} 
相關問題