Web API过滤器阻止其他过滤器被击中

时间:2012-11-24 20:49:15

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

当我添加HttpHeader过滤器时,其他过滤器停止工作。我不知道为什么。

BaseApiController.cs

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

HttpHeaderFilter.cs

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 override bool AllowMultiple
        {
            get { return true; }
        }
    }

    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 true; }
        }

        public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
        {
            return continuation().ContinueWith<HttpResponseMessage>((tsk) =>
                {
                    HttpResponseMessage response = tsk.Result;

                    response.Headers.Add(_name, _value);

                    return response;

                }, TaskContinuationOptions.OnlyOnRanToCompletion);;
        }
    }
}

Global.asax中

protected override IKernel CreateKernel()
        {


            return Bootstrap.Configure((kernel) =>
                {
                    kernel.Bind<IUnitOfWork>().To<UnitOfWork>();
                    kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>().InRequestScope();
                    //kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>().InScope(q => HttpContext.Current ?? StandardScopeCallbacks.Thread(q));
                    kernel.Bind<IEmailService>().To<EmailService>().WithConstructorArgument("templates", emailTemplates);
                    kernel.Bind<IPaymentMethod>().To<AuthorizeNetProvider>().Named("AuthorizeNet");
                    kernel.Bind<IPaymentMethod>().To<StripeProvider>().Named("Stripe");
                    kernel.Bind<List<IPaymentMethod>>().ToConstant(kernel.GetAll<IPaymentMethod>().ToList());

                    ServiceBindings.Register(kernel);
                    RepositoryBindings.Register(kernel);
                    ValidationBindings.Register(kernel);

                    kernel.BindHttpFilter<ApiValidationFilter>(System.Web.Http.Filters.FilterScope.Action)
                        .WhenControllerHas<ApiValidationAttribute>();

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

                    GlobalConfiguration.Configuration.Filters.Add(new ExceptionHandlingAttribute());

                    GlobalConfiguration.Configuration.DependencyResolver = new Common.Ioc.NinjectDependencyResolver(kernel);
                    GlobalConfiguration.Configuration.Formatters.Add(new CustomXmlFormatter());
                    GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
                });
        }

1 个答案:

答案 0 :(得分:0)

我添加了这个

TaskContinuationOptions.ExecuteSynchronously

而不是

TaskContinuationOptions.OnlyOnRanToCompletion 

现在工作正常。