2012-11-15 45 views
1

我不斷收到此錯誤,試圖綁定一個過濾器和一個web api控制器中的所有操作。我使用Ninject 3.0和ASP.NET MVC 4Web API和Ninject過濾器造成序列不包含任何元素

<Response> 
<Message>An error has occurred.</Message> 
<ExceptionMessage>Sequence contains no elements</ExceptionMessage> 
<ExceptionType>System.InvalidOperationException</ExceptionType> 
<StackTrace> 
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source) at Ninject.Web.WebApi.FilterBindingSyntax.FilterFilterBindingBuilder`1.<>c__DisplayClass15`1.<WithConstructorArgumentFromActionAttribute>b__14(IContext ctx, HttpConfiguration controllerContext, HttpActionDescriptor actionDescriptor) in d:\Projects\Ninject.Web.WebApi\src\Ninject.Web.WebApi\FilterBindingSyntax\FilterFilterBindingBuilder.cs:line 385 at Ninject.Web.WebApi.FilterBindingSyntax.FilterFilterBindingBuilder`1.<>c__DisplayClass12.<WithConstructorArgument>b__11(IContext ctx) in d:\Projects\Ninject.Web.WebApi\src\Ninject.Web.WebApi\FilterBindingSyntax\FilterFilterBindingBuilder.cs:line 363 at Ninject.Parameters.Parameter.<>c__DisplayClass6.<.ctor>b__4(IContext ctx, ITarget target) in c:\Projects\Ninject\ninject\src\Ninject\Parameters\Parameter.cs:line 60 at Ninject.Parameters.Parameter.GetValue(IContext context, ITarget target) in c:\Projects\Ninject\ninject\src\Ninject\Parameters\Parameter.cs:line 89 at 

BaseApiController.cs

[ExceptionHandling, ApiValidation, HttpHeader("X-Robots-Tag", "noindex, nofollow")] 
    public abstract class BaseApiController : System.Web.Http.ApiController 
    { 
     protected readonly IApiAuthenticationService ApiAuthenticationService; 

     protected BaseApiController(
      IApiAuthenticationService apiAuthenticationService) 
     { 
      ApiAuthenticationService = apiAuthenticationService; 
     } 
    } 

的Global.asax

kernel.BindHttpFilter<HttpHeaderFilter>(System.Web.Http.Filters.FilterScope.Controller) 
         .WhenControllerHas<HttpHeaderAttribute>() 
         .WithConstructorArgumentFromActionAttribute<HttpHeaderAttribute>("name", q => q.Name) 
         .WithConstructorArgumentFromActionAttribute<HttpHeaderAttribute>("value", q => q.Value); 

HttpHeaderFilter.cs

using System; 
using System.Net.Http; 
using System.Threading; 
using System.Threading.Tasks; 
using System.Web.Http.Controllers; 

namespace Tournaments.Models.Mvc.Filters 
{ 
    public class HttpHeaderAttribute : System.Web.Http.Filters.FilterAttribute 
    { 
     public string Name { get; set; } 
     public string Value { get; set; } 

     public HttpHeaderAttribute(string name, string value) 
     { 
      Name = name; 
      Value = value; 
     } 
    } 

    public class HttpHeaderFilter : System.Web.Http.Filters.IActionFilter 
    { 
     private readonly string _name; 
     private readonly string _value; 

     public HttpHeaderFilter(string name, string value) 
     { 
      _name = name; 
      _value = value; 
     } 

     public bool AllowMultiple 
     { 
      get { return false; } 
     } 

     public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation) 
     { 
      actionContext.Response.Headers.Add(_name, _value); 

      return continuation(); 
     } 
    } 
} 

回答

3

您的屬性在控制器上。在這種情況下,您必須使用WithConstructorArgumentFromControllerAttribute

+0

我不這麼認爲 –

+1

你試過了嗎?即使它沒有,那麼WithConstructorArgumentFromActionAttribute在你的情況下是錯誤的方法。 –

+0

@MikeFlynn Remo對Ninject有着豐富的經驗。無論哪種方式,你都需要解釋自己,而不是像「我不這麼認爲」這樣的嘟tering。像這樣的評論應該被標記(我還沒有完成和雷莫是太好了做) –

相關問題