Change default ASP MVC Request Header to add your own values

asp.net-mvc, c#, http-headers

Solution

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.

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

Problem

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 : Any ideas?

Original source

Related problems