2012-10-20 27 views
6

Tôi đã tự hỏi liệu có thể truy cập vào bộ điều khiển đang được thực thi (hoặc sắp được thực thi) trong phương thức SendAsync của DelegatingHandler không? Tôi dường như không thể tìm ra cách để truy cập vào nó, và tôi đoán nó là bởi vì nó thực hiện bên ngoài việc thực hiện điều khiển ...Truy cập vào thực thi bộ điều khiển hiện tại trong DelegatingHandler

Có thể tham khảo nó?

Trả lời

15

Không, bởi vì trình xử lý tin nhắn hoạt động trên số HttpRequestMessage hoặc raw HttpResponseMessage (trong trường hợp tiếp tục). Vì vậy, thực sự, không có khái niệm về "bộ điều khiển hiện tại đang thực hiện" với DelegatingHandlers vì trình xử lý tin nhắn sẽ được gọi trước khi gửi yêu cầu đến bộ điều khiển hoặc (một lần nữa, trong trường hợp tiếp tục) sau khi bộ điều khiển trả về phản hồi.

Tuy nhiên, nó thực sự phụ thuộc vào những gì bạn đang cố gắng làm.

Nếu bạn muốn biết bộ điều khiển nào yêu cầu sẽ được định tuyến, bạn có thể gọi thủ công cơ chế chọn nội bộ bộ điều khiển.

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

Cảm ơn, tôi có quy trình xác thực và sẽ rất tốt để kiểm tra loại bộ điều khiển để xem liệu thuộc tính tùy chỉnh có được xác định hay không, vì vậy tôi có thể tránh thói quen xác thực này. Điều này sẽ làm cho điều đó có thể. –

+0

tuyệt vời. có bạn có thể dễ dàng làm điều đó một khi bạn lấy một thể hiện của HttpControllerDescriptor –

+0

@ FilipW là có một cách 'tốt đẹp' để làm điều này mà không có nó ném một ngoại lệ? trong mã nguồn cuộc gọi đến 'SelectController' cuối cùng gọi' this._controllerInfoCache.Value.TryGetValue (controllerName, out controllerDescriptor) 'tuy nhiên' _controllerInfoCache' không thể truy cập công khai theo bất kỳ cách nào – wal

0

Mở rộng các giải pháp @GalacticBoy, nó sẽ là tốt hơn để sử dụng

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

Tùy thuộc vào những gì bạn làm với các thông tin có lẽ tốt của bạn với nhận được thông tin sau khi yêu cầu được thực thi. Ví dụ ghi nhật ký điều khiển/hành động được thực hiện.

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); 
     } 
    } 
} 
Các vấn đề liên quan