Bri*_*ins 8 asp.net asp.net-web-api
我想知道是否可以在DelegatingHandler的SendAsync方法中访问正在执行(或即将执行)的控制器?我似乎无法弄清楚如何访问它,我认为这是因为它在控制器执行之外执行...
可以参考吗?
Fil*_*p W 17
不,因为消息处理程序在原始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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4144 次 |
| 最近记录: |