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=""> <s> <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