迁移到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;
});
}
}
提前致谢
答案 0 :(得分:1)
如果通过依赖注入实例化LinkDecoratorHandler
,则可以注入IActionContextAccessor
的实例以获取当前ActionContext
。从那里,你可以create your own UrlHelper instance。