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

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

1 に答える 1

0

これを追加しました

TaskContinuationOptions.ExecuteSynchronously

それ以外の

TaskContinuationOptions.OnlyOnRanToCompletion 

そして今はうまくいきます。

于 2012-12-19T15:54:26.367 に答える