[英]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.