feanz
11/21/2013 - 12:02 PM

MVC Application Version Filter

MVC Application Version Filter

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
	public class CurrentVersionHeaderAttribute : ActionFilterAttribute
	{
		private const string _xVersion = "X-Version";

		private static readonly string _version = Assembly.GetExecutingAssembly().GetName().Version.ToString();

		public override void OnResultExecuted(ResultExecutedContext filterContext)
		{
			var headers = filterContext.HttpContext.Response.Headers;

			if (headers != null)
			{
				if (!headers.AllKeys.Contains(_xVersion))
				{
					headers.Add(_xVersion, _version);
				}
			}
		}
	}