Thursday, March 12, 2009

ASP.NET MVC "The parameters dictionary contains a null entry for parameter”

This (The parameters dictionary contains a null entry for parameter) exception was thrown at me and confused me for a while.

If you ever encounter it I can almost guarantee that you have a Routing-issue.  That is the way the ASP.NET MVC framework is figuring out which controller and which method to invoke.

In my case I had a controller action method that required two parameter but my routing table only allowed for one.

Here is a description on how to add your own, custom route rules.

And also here is a great tool to understand and debug your routing-table entries.

Finally – this webcast contains a discussion about how to change routes.

1 comments:

Bruno Salvino said...

You could also accept null values in yours action methods and return a "not found" view in case of null value, like this:

public virtual ActionResult Details(int? id) {

if (!id.HasValue)
return View(Views.NotFound);

var productDetail = productsRepository.GetProduct(id.Value);

if (productDetail == null)
return View(Views.NotFound);

return View(productDetail);
}

This way it won't give an exception.