10

Im trying to change all my ASP MVC HTTP response headers to have another value by default for implementing Pingback auto-discovery in my blog application.

The default header (on Cassini) is :

Cache-Control   private
Connection  Close
Content-Length  20901
Content-Type    text/html; charset=utf-8
Date    Fri, 20 Apr 2012 22:46:11 GMT
Server  ASP.NET Development Server/10.0.0.0
X-AspNet-Version    4.0.30319
X-AspNetMvc-Version 3.0

and i want an extra value added :

X-Pingback: http://localhost:4912/pingback/xmlrpcserver

I have googled a bit and found a neet solution : -- to derive from ActionFilterAttribute and override the OnResultExecuted method:

public class HttpHeaderAttribute : ActionFilterAttribute
    {

        public string Name { get; set; }
        public string Value { get; set; }

        public HttpHeaderAttribute(string name, string value)
        {
            Name = name;
            Value = value;
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            filterContext.HttpContext.Request.Headers.Add(Name, Value);
            base.OnResultExecuted(filterContext);
        }

    }

And then simply i put the attribute on my Controllers methods:

[HttpHeader("X-Pingback","http://localhost:4912/pingback/xmlrpcserver")]
        public ActionResult Index()
        {
            var allArticles = _repository.GetPublishedArticles(SortOrder.desc);
            return View(allArticles);
        }

When i runt the app i get the following error : enter image description here

Any ideas?

5
  • 1
    Have you tried IISExpress instead of Cassini? Commented Apr 20, 2012 at 23:25
  • @dtryon nope, i'll try it now and will let you know Commented Apr 21, 2012 at 11:08
  • the error it was indeed from Cassini ;) Commented Apr 21, 2012 at 12:00
  • Am I the only one who catches that you're adding headers to the request object...not the response? Commented Mar 1, 2013 at 19:02
  • @LostInJavaScriptLand so finally somebody spotted it :-p . in my real app i was on the response not request but here is a typo :-p Commented Mar 1, 2013 at 19:04

3 Answers 3

6

I know this post is old...but wanted to point out that while OnResultExecuting is the proper method to be doing this from, the original post shows that he was trying to add headers to the request. One does not simply add headers to a request and expect them to show up in the response. ;-)

Also, the proper way to add headers to a response...that also works in Cassini...is to use the following:

filterContext.HttpContext.Response.AddHeader("X-My-Request-Header", "works in cassini");
Sign up to request clarification or add additional context in comments.

3 Comments

Just to note that the question refers to adding the header into the Request, not the Response.
Actually Elliveny, the first line of the question states he's trying to modify response headers, not request headers. His examples were incorrectly trying to modify the request headers.
For MVC Core 2.x, the method is now: filterContext.HttpContext.Response.Headers.Add().
4

I believe your problem may be simply that you are trying to modify the headers too late, since you're doing it in OnResultExecuted. Try overriding OnResultExecuting instead.

http://msdn.microsoft.com/en-us/library/system.web.mvc.actionfilterattribute.onresultexecuting.aspx

2 Comments

just tried this now, still the same "platform" error. i will give iis express a go
so you are right sir! it should have been OnResultExecuting and both IIS express!
2

This may or may not work (obviously if it doesn't I'll delete the answer for future users). It sounds like from the exception Operation is not supported by this platform, that Cassini many not support custom headers (which would be quite strange, but a possibility). What I would suggest is to make sure you are using Visual Studio 2010 SP1, then install IIS Express (which is an upgrade to Cassini and is much more like real IIS), and then switch your project to use IIS Express and see if you get the same exception.

Switching from Cassini to IIS Express

Additionally, you may want to review Why does HttpCacheability.Private suppress ETags? as it may also give you an alternative solution.

2 Comments

thanks Erik! it should have been both IIS express to avoid the platform issue, and then use OnResultExecuting !
yes, it seems that Cassini was to blame here. and what bhamin suggested to use the other event proved right as well

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.