ASP.NET MVC ImagePixel ActionResult

July 10, 2011

tags: , , ,
2 comments

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

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

2 comments

  1. Rotem BloomAugust 8, 2011 ב 1:37 PM

    נחמד ביותר תודה על השיתוף :-)

    Reply
  2. paintball guyNovember 13, 2012 ב 4:28 PM

    just what i needed… using this to track my email views

    Reply