DelegatingHandler不执行ASP.Net Web Api [英] DelegatingHandler not executing ASP.Net Web Api

查看:55
本文介绍了DelegatingHandler不执行ASP.Net Web Api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我在Web Api应用程序中遇到了奇怪的行为

today i encountered a strange behavior in my Web Api application

protected void Application_Start() {

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

我的DelegatingHandler看起来像这样.

And my DelegatingHandler looks like this.

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.

我的nugetpackages.config文件如下:

My nuget packages.config is the following:

<?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

推荐答案

您所做的正确.因为 DelegatingHandler 仅在您调用Web API Controller 操作时运行,所以可能会出现问题.例如:

What you have done is correct. The problem might be happening because the DelegatingHandler only runs when you call a Web API Controller action. For example:

这将调用您的消息处理程序,因为它是ApiController.

This will invoke your Message Handler, because it's an 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();
    }
}

确保您正在调用Web API控制器操作,否则调试器将无法达到您的断点.

Make sure you are calling a Web API Controller action, otherwise the debugger will not hit your break point.

这篇关于DelegatingHandler不执行ASP.Net Web Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆