DCSIMG
<-- +1 script --> July 2011 - Posts - .Net && Beyond

.Net && Beyond

Taking about .NET and much more

July 2011 - Posts

ASP.NET MVC ImagePixel ActionResult

Just wanted to create a web logger in MVC. The idea is the same as Google analytics that a client will send data from the "client side" to the server. The server will save the data and return “Image Pixel” just like any other normal web logger. In order to do that I have created an ImagePixel ActionResult.

The server logger method will look something like that:

public ActionResult Logger(string trackingData)
{
    Log(trackingData);
    return new ImagePixelResult();
}

The ImagePixelResult is defined like this:

 
    public class ImagePixelResult : ActionResult
    {
        private static readonly byte[] Imgbytes = Convert.FromBase64String("R0lGODlhAQABAIAAANvf7wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==");
        
        public override void ExecuteResult(ControllerContext context)
        {
            HttpResponseBase httpResponse = context.HttpContext.Response; 
            httpResponse.ContentType = "image/gif";
            httpResponse.AppendHeader("Content-Length", Imgbytes.Length.ToString());
            httpResponse.Cache.SetLastModified(DateTime.Now);
            httpResponse.Cache.SetCacheability(HttpCacheability.NoCache);
            httpResponse.Expires = -1500;
            httpResponse.Cache.SetNoStore();
            httpResponse.ExpiresAbsolute = DateTime.Now.AddYears(-1);
            httpResponse.BinaryWrite(Imgbytes);
        }
    }

Clean and simple :)

 

Keep Writing, Compiling, and Debugging.

Alon Nativ

Posted: Jul 10 2011, 08:36 PM by Alon Nativ | with 2 comment(s)
תגים:, , ,