我尝试使用自定义消息处理程序向我的WebAPI添加路由
config.Routes.MapHttpRoute(
name: "TestRoute",
routeTemplate: "Protected/TestMe",
defaults: null,
constraints: null,
handler: new CustomHandler()
);
处理程序代码
internal class CustomHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpResponseMessage respMsg = new HttpResponseMessage();
respMsg.Headers.Add("MyCustomHeader", "Test");
respMsg.StatusCode = HttpStatusCode.NoContent;
return Task.FromResult(respMsg);
}
}
但是每当我访问所述路线时,我都没有得到我的自定义标题或者是NoContent代码,而是我从路线接收结果,但是如果我将处理程序添加为全局的
config.MessageHandlers.Add(new CustomHandler());
它工作并返回我的自定义标头和NoContent状态代码 我在路线设置中缺少什么?