Returning JsonResult From ASP.NET MVC 2.0 Controller and Unit Testing

* I’m adding this comment after the post has been written to let anyone coming here know of another great post that goes further to explain no only what I’ve done here, but also two other ways including Mock and using Json Serialization.  The Post is written by Ashic Mahtab, aka “HeartattacK” on the forums.  His well written and informative article is here:  ASP.NET MVC – Unit Testing JsonResult Returning Anonymous Types. This post will show how to return a simple Json result from an ASP.NET MVC 2.0 web project.  It will show how to test that result inside a unit test and essentially pick apart the Json, just like a JavaScript (or other client) would do.  It seems like it should be very simple (and indeed, once … Continue Reading

Using Reflection in C# to Figure Out if an Integer List Has Values

So, I’ve struggled some with this and did not have much help with searching. Here is the problem. You have a declaration that looks like this: 1: public partial class CompanyQuery { 2: public List<int> CompanyTypeIds { get; set; } 3: } Then, you need to figure out if CompanyTypeIds is populated using reflection. … Continue Reading

Using Method.Invoke to avoid a lengthy Case Statement in C# (Using Reflection)

So, I asked the following question in at The problem is, I’ve got 30 methods that all have the same signature, but have different implementations.  Mike Banavige (the lead moderator at the forums) suggested I look at the following article on reflection:  Turns out, that helped my solve my problem exactly.  Rather than go through a lengthy line by line explanation, I’m going to post some of my before and after code.  It’s pretty clear what is happening, but I thought it would be nice to show a real example. … Continue Reading


Get every new post delivered to your Inbox

Join other followers: