Development – Leet 4 Life

Software Development Code Samples and More

Skip to: Content | Sidebar | Footer

Category: C#. NET

Serializing NameValueCollection to JSON in ASP.NET MVC Web APIs

10 June, 2013 | ASP.NET, C#. NET | By: Michael Merrell

Recently I had the need to serialize a NameValueCollection object through MVC’s WebAPIs. However the inherit object that is provided in the Specialized Collections .NET Framework does not play nicely with JSON serialization. Because of this I ended up have to write my own set of custom classes for serializing NameValueCollection from JSON to be used inside the functionality of my Web APIs. This code is simple and gets the job done very well. Then the client side JavaScript: Read more about my problem of Serializing NameValueCollection to JSON using MVC Web API

Displaying TIFF Images in ASP.NET

3 October, 2012 | ASP.NET, C#. NET | By: Michael Adams

I needed to display TIFF images that were stored in the database. Unfortunately it is not as simple as just setting the MIME Type and writing out the binary. The reason for this is because the TIFF image type is only supported in IE currently. So I asked Google and the overwhelming response I saw was to convert the TIFF into another image type and then write out the binary to the response stream. Here is my take on the solution: Written in C# (the image is sent via a Generic Handler hence the “context.Response”): It should be noted that [Read More…]

Firing JavaScript only when inside an ASP.NET Ajax Postback

29 June, 2012 | ASP.NET, C#. NET, jQuery | By: Michael Merrell

There are times where you need to fire a JavaScript event only when you are posting inside of an ASP.NET AJAX post back event. In my example below I am needing to fire a function used for a document ready jQuery event. This event only needs to be fired on AJAX post backs because on normal posts back it will already be handled by jQuery, however it will not be fired when executed inside of an AJAX post back unless told to. The following JavaScript is a pretty standard script for firing a set of functionality when a page has [Read More…]

Compress and Decompress String Using GZIP Compression

29 May, 2012 | C#. NET | By: Michael Merrell

Recently I ran into an issue where data I was sending into a cookie was causing the following exception: “A potentially dangerous Request.Cookies value was detected from the client”. Rather than removing the validation from the page so that it wouldn’t threw an error I looked into another method in order to keep the page validated and keep my functionality in place. I decided that the best way to get the text that I wanted to transfer through a cookie, in this case an exception stack trace, would be to compress the string and then simply send the compressed string [Read More…]

Convert One Entity Object to A Similar Entity Object

30 April, 2012 | C#. NET, Entity Framework | By: Michael Merrell

Not too long ago I came across the need to be able to convert one entity object to another similar entity object for use in display purposes and mapping. Even though these two objects were nearly identical they had over two hundred fields in them so writing a method to simply set properties equal to each other would have created a huge method and a higher chance of error. Using reflection I created a nice little thinned down method that will take in your source object, the output object type, and a reference to the instantiated output object. Then using [Read More…]