Category Archives: .NET

Thinking Outside The Box

Boxes
Boxes

I could really write a few things with that title. As developers we often take the path of least resistance or of most familiarity. Usually this is good because we are using best practices or some other coding standard. Sometimes, these well worn trails are not always the best.

Read the rest of this entry

My Json Date Lost Its Offset

What Happened

My golf buddies lost in a sand trap

I was using NewtonSoft like so many of us for Json Serialization and Deserialization. In this case I was trying to make sure our DateTimes were being returned in a proper ISO8601 format, defaulting to UTC unless a flag was set. If the flag was set it would then use the offset associated with a customer’s location, never to use the browser’s offset.

Sadly it was always rendering in Postman and SoapUI in my local time zone. Even when I would debug through the code, the return Ok(jsonObject); had the expected time zone and format.

Read the rest of this entry

Detect and Reflect a Generic List

A pool table at the Biltmore Estate
A pool table at the Biltmore Estate

We have a project that uses reflection quite a bit. If you use any kind of mapper chances are you too are using reflection. One gotcha with reflection is the name of the properties, unless you make some other infrastructure to support mapping one name to another chances are you won’t get the data you desire.

Read the rest of this entry

The Expando Object

Mushrooms
Just some mushrooms

The Expando object has been around for a while. In fact it was in .NET 4.0, in the ancient days of 2009.

I am sure, that back then, I must have thought that it was neat. Maybe a bit like JavaScript, but I never had a use for it until recently.

Read the rest of this entry

Linq to CSV

Linq to CSV

Something Old

Something old

This post is about something old, something I should have posted about a long time ago.  A few years ago I had to use a CSV file as a data source for some process.  I had forgotten all about it until recently when I was looking through some old projects, they definitely brought back some memories.

I do remember thinking there has to be a better way to access a CSV file more like a database and have it populate an object.  I started to get my frame of mind all set to come up with a solution when I thought how dumb it is to re-invent the wheel.  So I did a search first, and found the LinqToCsv package.

Read the rest of this entry

Visual Studio Won’t Open CSHTML Files

I had an issue one day where Visual Studio would not open my CSHTML files.  It would give me the following error.

The operation could not be completed.  Invalid pointer.

So naturally I took to Bing.  I eventually found the work around on StackOverFlow.

The fix for me was not the question’s accepted answer but one further down.  I deleted the contents in the %LocalAppData%\Microsoft\VisualStudio\14.0\ComponentModelCache folder.

After that I never had this issue again.

Change Login Using Windows Authentication

Recently I had a need to allow users to change their login on an MVC site that used Windows authentication.  Of course we did not want the user to log out of their machine to do it.  The trick of course involved sending a 401 response, but how to do that and not get stuck in an endless loop.

The 401 Loop

It seemed simple enough, in fact too simple.  You just return a 401 challenge and have them move on to their previous page.  The 401 response is what presents the user with the Windows login popup.  In reality the 401 response acts like a redirect on itself, so you get two page loads and it forgets any variables you set.

Well that simply would not do. Read the rest of this entry

The Magic of Config Transformations

Fog, trees, and a lightWe have all used the magic of the Web.config transformations.  This is when we have our Web.config and connected to that is a version that does some work for release and one for debug; Web.Release.config and Web.Debug.config.  These are a great way to automatically have the correct target use the correct settings, especially connection strings.  Whether we use that or not and just leave it as something that looks cool is a different thing.

Did you know you can get this magic for your console applications as well?  I know, a web developer doing console apps?  As an enterprise one sometimes I find it is the best solution to some problems.  While it seems like a small thing I wished I had it in the console applications, I am happy to say that I have found it!

Read the rest of this entry

MVC 5 Placeholders, Prompts and Watermarks

I have noticed an issue with the MVC project in Visual Studio that I am sure others have seen too.  I say this because I have seen a number of solutions in StackOverflow and the ASP.Net forums.  This post is not about some great way to write your own extensions and automagically have the placeholder set in the HTML that is displayed to the user.  No, this is a much simpler method.

Just manually set the placeholder in your Razor syntax.  Chances are you are going in there anyway to tweak a few things instead of using what Visual Studio scaffolded for you.  I would still recommend setting the Prompt in your model though, I suspect that one day this will get fixed or you might actually need it to be automatically generated.  In fact I’d say that if you are writing your own Edit Templates you should include the small extra step to make it auto-generated.

If you are not doing that, here is the easiest fix or work around.

Read the rest of this entry

Collection Was Modified; Enumeraton Operation May Not Execute

Some Background

This was a very interesting bug for me to track down.  Initially it did not bubble up as the error that is in this title.  Initially it revealed itself as an out of bounds error when I tried to set IsOpen to true for a ContextMenu in a Silverlight project.  After lots of debugging I finally landed on this culprit.

The Task

The task I was trying to accomplish was pretty straight forward.  Given a List<T> remove any duplicate entries based on a given object.  Again, a straight forward task, or so I thought.


foreach(MyClass item in myListOfMyClass)
{
    if(item.SomeValue == objectOfMyClass.SomeValue)
    {
        myListOfMyClass.Remove(item);
    }
}

Read the rest of this entry