Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8864225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:11:26+00:00 2026-06-14T16:11:26+00:00

Im trying to add a header attribute to a controller, but the Response is

  • 0

Im trying to add a header attribute to a controller, but the Response is null with the HttpActionContext property. Am I doing something wrong?

Controller.cs

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

HttpHeaderFilter.cs

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

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

        return continuation();
    }
}

Global.asax

kernel.BindHttpFilter<HttpHeaderFilter>(System.Web.Http.Filters.FilterScope.Controller)
                       .WhenControllerHas<HttpHeaderAttribute>()
                       .WithConstructorArgumentFromControllerAttribute<HttpHeaderAttribute>("name", q => q.Name)
                       .WithConstructorArgumentFromControllerAttribute<HttpHeaderAttribute>("value", q => q.Value);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:11:27+00:00Added an answer on June 14, 2026 at 4:11 pm

    It would be simpler for you to derive from web api’s ActionFiterAttribute class and add the header to the response instead of implementing an action filter from scratch using IActionFilter.

    [Edited]
    For the above scenario, try doing the following:

    return continuation().ContinueWith<HttpResponseMessage>((tsk) =>
                    {
                        HttpResponseMessage response = tsk.Result;
    
                        response.Headers.Add(...)
    
                        return response;
    
                    }, TaskContinuationOptions.OnlyOnRanToCompletion);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add a value to the header for a URL request. Something
I am trying to add a header to my listview but only the header
I'm trying to add an header file to dev-C++ but when I compile it
We are trying to add a custom header (X-Robots-Tag) for sitemap files in IIS
I am trying to write header file. I can write simple headers like add(int
Im new to asp.net mvc. I'm trying add new model class but it got
Trying to add a custom header item to a Lotus Notes email item, from
Noob question. :) I'm trying to add Margin to a Header through out my
I have a <table> that has a header row. I'm trying to add an
I want to add a header in my listview but it's getting a forceclose.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.