访问DelegatingHandler中的IUrlHelper

时间:2018-04-04 05:39:38

标签: asp.net-core .net-core

迁移到ASP.Net核心后,以下处理程序不起作用。我无法像以前那样看到如何从IUrlHelper访问HttpRequestMessage,并且无法找到包含相关扩展方法的包。

使用config.MessageHandlers.Add(new LinkDecoratorHandler());

添加处理程序

有人可以帮忙吗?

 public class LinkDecoratorHandler : DelegatingHandler
    {

     protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage 
        request, CancellationToken cancellationToken)
        {
        return base.SendAsync(request, cancellationToken)
            .ContinueWith(task =>
            {
                var response = task.Result;

                if (!(response.Content is ObjectContent))
                {
                    return response;
                }

                var entity = (response.Content as ObjectContent).Value as ILinkedEntity;

                var enumeration = (response.Content as ObjectContent).Value as IEnumerable<ILinkedEntity>;

                if (entity != null || enumeration != null)
                {
                    //no longer available
                    var helper = request.GetUrlHelper();

                //blah
                }

                return response;
            });
        }
}

提前致谢

1 个答案:

答案 0 :(得分:1)

如果通过依赖注入实例化LinkDecoratorHandler,则可以注入IActionContextAccessor的实例以获取当前ActionContext。从那里,你可以create your own UrlHelper instance