简体   繁体   English

DelegatingHandler 不执行 ASP.Net Web Api

[英]DelegatingHandler not executing ASP.Net Web Api

today i encountered a strange behavior in my Web Api application今天我在我的 Web Api 应用程序中遇到了一个奇怪的行为

protected void Application_Start() {

    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    GlobalConfiguration.Configuration
        .MessageHandlers.Add(new DummyMessageHandler());
}

And my DelegatingHandler looks like this.我的 DelegatingHandler 看起来像这样。

public class DummyMessageHandler : DelegatingHandler {

    protected override Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request, CancellationToken cancellationToken) {

*       if (request.Headers.Authorization.Scheme == "Basic")
            Thread.CurrentPrincipal = new GenericPrincipal(
                new GenericIdentity("Authenticated"), new string[0]);

        return base.SendAsync(request, cancellationToken);
    }
}

The problem I encountered was that the delegating handlers are not being executed.我遇到的问题是委托处理程序没有被执行。 I have a breakpoint in the line marked with a * and the execution of my code never stops there.我在标有 * 的行中有一个断点,并且我的代码的执行永远不会停止。

My nuget packages.config is the following:我的 nuget packages.config 如下:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.AspNet.Mvc" version="4.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.Razor" version="2.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.0.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebApi" version="4.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebApi.Client" version="4.1.0-alpha-120809" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebApi.Core" version="4.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebPages" version="2.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" />
  <package id="Newtonsoft.Json" version="4.5.8" targetFramework="net40" />
  <package id="WebGrease" version="1.1.0" targetFramework="net40" />
</packages>

I'm looking at this for a long time, can you point me to something I am missing ?我看这个很久了,你能指出我遗漏的东西吗? Thank you谢谢

What you have done is correct.你所做的是正确的。 The problem might be happening because the DelegatingHandler only runs when you call a Web API Controller action.问题可能会发生,因为DelegatingHandler仅在您调用 Web API 控制器操作时运行。 For example:例如:

This will invoke your Message Handler , because it's an ApiController.这将调用您的 Message Handler ,因为它是一个 ApiController。

public class ValuesController : ApiController
{
    // GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

This will NOT , because it's just a Controller.这不会,因为它只是一个控制器。

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Title = "Home Page";

        return View();
    }
}

Make sure you are calling a Web API Controller action, otherwise the debugger will not hit your break point.确保您正在调用 Web API 控制器操作,否则调试器将不会到达您的断点。

you should register your handler in the WebApiConfig file, not in the global.asax你应该在 WebApiConfig 文件中注册你的处理程序,而不是在 global.asax

 public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {

            config.MapHttpAttributeRoutes();
            config.MessageHandlers.Add( new DummyMessageHandler());
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ASP.NET Core 5.0 Web API 中实现 DelegatingHandler? - Implement DelegatingHandler in ASP.NET Core 5.0 Web API? 在 ASP.NET Core Web API 中注册新的 DelegatingHandler - Registering a new DelegatingHandler in ASP.NET Core Web API 在DelegatingHandler ASP.NET Web API中添加BearerToken授权标头 - Addition of BearerToken Authorization Header in DelegatingHandler ASP.NET Web API 在ASP.Net Web API中的日志记录DelegatingHandler中读取HttpRequestMessage.Content时丢失 - HttpRequestMessage.Content is lost when it is read in a logging DelegatingHandler in ASP.Net Web API ASP.NET Web API中的Castle Windsor / DelegatingHandler / IPrincipal依赖注入(DI)/控制反转(IoC) - Castle Windsor/DelegatingHandler/IPrincipal Dependency Injection (DI)/Inversion of Control (IoC) in ASP.NET Web API DelegatingHandler应该如何进行异步调用(ASP.NET MVC Web API)? - How should a DelegatingHandler make an async call (ASP.NET MVC Web API)? 如何在 ASP.NET MVC 4 Web API 中测试自定义 DelegatingHandler? - How can I test a custom DelegatingHandler in the ASP.NET MVC 4 Web API? 为什么调用ASP.NET MVC Controller不执行DelegatingHandler? - Why does a call to an ASP.NET MVC Controller not execute a DelegatingHandler? ASP.NET Web Api配置不执行消息处理程序的特定路由 - ASP.NET Web Api configuring a specific route not executing message handler Web表单中的ASP.NET Web Api - ASP.NET Web Api in Web forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM