Access to the current controller running in the DelegatingHandler

I was wondering if it is possible to access the controller that is running (or should be running) in the SendAsync method for the DelegatingHandler? I can’t figure out how to access it, and I understand it because it runs outside of the controller execution ...

Is it possible to refer to it?

+6
source share
3 answers

No, because message handlers work with raw HttpRequestMessage or raw HttpResponseMessage (if continued). So there really is no concept of a “current controller controller” with DelegatingHandlers , because message handlers will be called before sending a request to the controller or (again, in case of continuations) after the controller returns a response.

However, it really depends on what you are trying to do.

If you want to know which controller the request will be redirected to, you can manually call the mechanism that the controllers will internally select.

 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 null (ie controller not found), it will throw an exception var descriptor = controllerSelector.SelectController(request); // continue return base.SendAsync(request, cancellationToken); } } 
+15
source

Extending @GalacticBoy's solution, it would be better to use

 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 null (ie controller not found), it will throw an exception var descriptor = _controllerSelector.SelectController(request); } catch { // controller not found } // continue return base.SendAsync(request, cancellationToken); } } 
0
source

Depending on what you do with the information, you may be fined for receiving information after the request. For example, logging the controller / action being performed.

 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); } } } 
0
source

Source: https://habr.com/ru/post/928161/


All Articles