【问题标题】:The inner handler has not been assigned using WebApi Delegating handler尚未使用 WebApi Delegating 处理程序分配内部处理程序
【发布时间】:2012-12-05 01:09:20
【问题描述】:

我遇到了 WebApi 在我的代码中抛出异常的问题:

public class WebApiAuthenticationHandler : DelegatingHandler
    {
        private const string AuthToken = "AUTH-TOKEN";

        protected override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {                  
            var requestAuthTokenList = GetRequestAuthTokens(request);
            if (ValidAuthorization(requestAuthTokenList))
            {
                // EXCEPTION is occuring here!....
                return base.SendAsync(request, cancellationToken);
            }

            /*
            ** This will make the whole API protected by the API token.
            ** To only protect parts of the API then mark controllers/methods
            ** with the Authorize attribute and always return this:
            **
            ** return base.SendAsync(request, cancellationToken);
            */
            return Task<HttpResponseMessage>.Factory.StartNew(
                () =>
                {
                    var resp = new HttpResponseMessage(HttpStatusCode.Unauthorized)
                    {
                        Content = new StringContent("Authorization failed")
                    };

                    //var resp = new HttpResponseMessage(HttpStatusCode.Unauthorized);                                                                                   
                    //resp.Headers.Add(SuppressFormsAuthenticationRedirectModule.SuppressFormsHeaderName,"true");
                    return resp;
                });
        }

线上发生异常:

base.SendAsync(request, cancellationToken);

我不知道如何解决这个问题。我的路由表中有以下内容:

    routes.MapHttpRoute("NoAuthRequiredApi", "api/auth/", new { Controller = "Auth" });
    routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional }, null, new WebApiAuthenticationHandler());

发生这种情况的路由是 DefaultApi 路由。任何帮助都非常感谢....

【问题讨论】:

    标签: asp.net asp.net-web-api asp.net-web-api-routing


    【解决方案1】:

    找到答案here 和示例处理程序here

    您需要设置您希望将请求传递到的 InnerHandler。

    只需将其添加到您的构造函数中:

    public class WebApiAuthenticationHandler : DelegatingHandler
    {
        public WebApiAuthenticationHandler(HttpConfiguration httpConfiguration)
        {
            InnerHandler = new HttpControllerDispatcher(httpConfiguration); 
        }
    

    并在创建新实例时传入对 GlobalConfiguration 的引用:

    routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional }, null, WebApiAuthenticationHandler(GlobalConfiguration.Configuration));
    

    【讨论】:

    • 在 ASP Core 2.1 这似乎工作 'InnerHandler = new HttpClientHandler();'
    • 我发现我必须在构造函数中进行赋值,否则出现只读错误。
    • 顶部的两个链接都已失效。有人更新了吗?
    【解决方案2】:

    有时您应该检查您请求的 RESTful Url 是否确实存在于您的控制器中。我曾经遇到过这种由错误的 URL 匹配引起的异常。谢谢。

    【讨论】:

      猜你喜欢
      • 2015-06-23
      • 1970-01-01
      • 2012-08-14
      • 2023-02-21
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多