Custom Error Handling In MVC 5

Creating a custom error page in MVC 5 is not too difficult.  If you want to have one that is server farm friendly, well that is not too tough either.  I created a little demo project for doing custom errors that passes a model around.  To be effective in handling uncaught errors we will need to use the Global.asax Application_Error method.  You can also use this method to send errors that you catch but just cannot handle or fail out of gracefully.

There are a number of reasons why you may want to do custom errors like I do below.

  • You are in a server farm or other environment where Session based objects will not work
  • Display a user friendly message based on the error received
  • Display a user friendly message based on an HTTP Code (if one was returned)
  • Log the error
  • Send a notification of the error

Read the rest of this entry

The Danger of Null Values

I made a goof ball mistake today.  I was using LINQ to query a List<> and return the ToString() of the result.  As usual I was using FirstOrDefault() and I simply added .ToString() to the end of it.

fieldValue = (from d in customFields where d.CustomFieldId == fieldId select d.data).FirstOrDefault().ToString();

Of course things worked swimmingly until the Default (null) was hit. At that point I got an Object not set to an instance error.  I had to laugh at myself.

var fieldData = (from d in customFields where d.CustomFieldId == fieldId select d.data).FirstOrDefault();
fieldValue = fieldData != null ? fieldData.ToString() : "";

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

Enabling Power User Mode In Opera

Opera has a neat feature for its settings called Power User mode.  What is even neater is how you enable this mode.

  1. Bring up the settings tab
  2. Type in that old game code with your arrow keys Up Up Down Down Left Right Left Right
  3. Then type B A to finish the code

Just to be clear the whole code is the arrow keys for direction followed by B and then A, you don’t need to hold shift down or anything.

Up Up Down Down Left Right Left Right B A

One of the really cool things you can do in power user mode is remove the search box from the Speed Dial page.  Personally I prefer Bing but right now you cannot change this box from Google.  So removing it gives me more immediate space in my Speed Dial.

Dragging Files To OneNote

I just learned something really cool about OneNote.  I was not sure how it would respond if I did a drag and drop of a PDF file to a OneNote page.  The result is impressive.  Well done OneNote team!

Insert Link, Insert a copy of the file onto the page, Insert the file as a printout so I can add notes to it

Drag and Drop Dialogue

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

Left or Right Curly Braces

Should you put your curly braces on the left or right?

function foo(){
    //My curly brace is on the right.
}

function bar()
{
    //My curly brace is on the left.
}

Read the rest of this entry

One Advantage of CodeWriter

A few weeks ago I posted about WebMatrix, which is a really nice light weight tool if you don’t want to fire up full Visual Studio.  I also indicated that I was using CodeWriter.  I have noticed one real advantage that CodeWriter has over WebMatrix.  I can run CodeWriter on my Surface 2.  My Surface 2 is not a pro version so I cannot run Visual Studio on it, and in turn I cannot run WebMatrix on it.

I will admit, I do not do a whole lot of dev work on my Surface 2.  In fact I normally write something up in CodeWriter so I can copy/paste it into OneNote or test something really small.

The JavaScript Object Literal

I have been really trying to beef up my JavaScript skills lately.  One really interesting aspect from this experience is how cool I find the object literal.

You can use it to create really any kind of data structure you desire.  It can hold the primitive types like strings and numbers, it can hold objects like arrays or other object literals, and it can store functions.  I created a fiddle at JSFiddle to demonstrate the object literal.

Here is the JavaScript for my object literal which I call oPerson.

var oPerson = {
    firstName: "John",
    lastName: "Doe",
    age: Math.floor((Math.random() * 33) + 17),
    "favorite color": "Blue",
    Interests: {
        movies: ["The Blob", "History of the World Part 1", "Ferris Bueller's Day Off"],
        bands: ["Lacuna Coil", "Soft Cell", "Naked Eyes"]
    },
    greeting: function () {
        return "Hello, my name is " + this.firstName + " " + this.lastName + ".";
    }
};

Read the rest of this entry

Some History Of I, J, And K

I learned a little bit of geek history today.  A lot of us have used the variable I (or i rather) in our loops, and sometimes for nested loops we would use j and then k.  Do you know where that started?

Apparently it started in FORTRAN.  The variables i, j, and k were to be only used for integer type variables.  I am sure there is more to it than that but a quick search gave two different sources that confirm it.

Wikipedia

StackOverflow

I know this is nothing ground breaking or anything, I just thought it was something neat.

Follow

Get every new post delivered to your Inbox.

Join 250 other followers