0

当我添加 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);;
        }
    }
}

全球.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);
                });
        }
4

1 回答 1

0

我添加了这个

TaskContinuationOptions.ExecuteSynchronously

代替

TaskContinuationOptions.OnlyOnRanToCompletion 

现在工作正常。

于 2012-12-19T15:54:26.367 回答