3

I am trying to insert an header in my controller according with the parameter sub, but with no success. I have tried these two lines, here's the code:

public ActionResult Index(string sub)
{
    if (!string.IsNullOrEmpty(sub))
    {
        HttpContext.Response.Headers.Add("sub", sub);
        Response.AddHeader("sub", sub);
    }

    return View();
}
3
  • Try this HttpContext.Current.Response.AppendHeader("sub",sub); OR Try this HttpContext.Response.AppendHeader("sub",sub); Commented May 28, 2013 at 12:15
  • possible duplication: stackoverflow.com/questions/10765725/… Commented May 28, 2013 at 12:18
  • Do you want this header to be accessible in all pages? Commented May 28, 2013 at 12:20

1 Answer 1

8

This may work for you..

public ActionResult Index(string sub)
{
    if (!string.IsNullOrEmpty(sub))
    {
        HttpContext.Response.AppendHeader("sub", sub);

    }
return View();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this worked for me, but now I have another problem. My header is like an array, because previously I have setted the value "0" in it. How can I clear this header before setting it again? Now the header is like: "xxxxx, 0" where 0 is the previous value.
Check if header exists first using Headers.AllKeys() and remove if it already exists..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.