Skip to main content

Posts

Showing posts from January, 2015

Return JSON straight up (not a string) in ApiController

The Problem: Had a hard time returning a plain JSON object from an ApiController (MVC4).  Instead, I'm getting the JSON object as a string. For example: "{\"dobSP\":\"90.25\",\"latestSP\":\"2025.9\",\"dobStampPrice\":\"0.15\",\"latestStampPrice\":\"0.49\",\"dobStampImage\":\"http://media1.s-nbcnews.com/j/msnbc/Components/Photos/051207/051207_stamp_news_hmed.grid-4x2.jpg\",\"latestStampImage\":\"http://media1.s-nbcnews.com/j/msnbc/Components/Photos/051207/051207_stamp_news_hmed.grid-4x2.jpg\",\"dataUpdated\":\"1/8/2015 11:27:48 AM\"}" What I've Learned: My Get method (in the controller) was specified to return a string object.  What I needed to return was a HttpResponseMessage type.   The Fix: I simply created a method that created the response for me:         private HttpResponseMessage ConvertToHttpResponse(string t...

Linq to DataTables - The Easy Way

The Problem: Couldn't easily read an excel file and query it programmatically. Using JSON.Net took a little longer than I thought it would. Wanted to brush up on my Linq skills. This was a rush job The Solution: Utilized "Excel Data Reader", which allowed me to read Excel data and converted it to a DataSet Used Linq to query the data easily, and produced manageable code (for me anyway) Used JSON>NET to serialize my code and write to the DOM Code Samples:             DataRow dr = ds.Tables[0].Rows.Cast<DataRow>()                 .Where(row => row["Column1"] == DBNull.Value)                 .FirstOrDefault(); In this example, I'm wrote a simple Linq query which returned all rows in the DataTable what has any data in the first column. Not the most sophisticated, but you get the point. What I've Learned: Origina...