Image Image Image Image Image
Scroll to Top

To Top

2013 Januar

[Gist] ASP.NET MVC bind String property as string.Empty instead of null

On 27, Jan 2013 | 3 Kommentare | inAOP | vonJohannes Hoppe

Currently I’m migrating a good-old ASP.NET MVC v1 project to v4.
In ASP.NET MVC 1.0 the default behavior of Model Binding was to initialize strings to String.Empty. Imagine this simple method:

public ActionResult Hello(string name)
    if (name.Contains("Bob"))
        return Content("Hi my friend!");
    return Content("Hi stranger!");

However, later versions of ASP.NET MVC initialize Strings to null. This results in some nasty NullReferenceExceptions all over the code. On the one hand this change of the framework might be reasonable. The default value of a String is null. But on the other hand we should try to avoid null values at all. As soon as we start to tolerate null values we are going to clutter the code with guards against them:

Vorträge und Termine 2013

On 18, Jan 2013 | 6 Kommentare | inGeneral | vonJohannes Hoppe

Mit insgesamt 12 Vorträgen und tollem Feedback war das Jahr 2012 für mich als Sprecher ein voller Erfolg. Ich bin gespannt, ob ich eine vergleichbare Serie dieses Jahr wiederholen kann. Folgende Liste wird laufend aktualisiert:


Tags | ,