Reputation: 5408
I'm getting the following error a lot when the Google bot comes by:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Show(Int32)' in 'someclass'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
I was wondering if it would be possible to have the application throw 404's instead of missing parameter exception in this case.
Thanks!
Update to clarify what I want is that all cases for this particular error throw a 404 error instead of a 500. Preferably by writing a wrapper of some kind that only catches this error.
Upvotes: 13
Views: 14981
Reputation: 12221
It's not a new article, and I can't promise that there's no better way to do it in the recent MVC builds, but I think this is a pretty good possibility :)
I believe you should be able to write a custom model binder, that will check if the ID property is present and is required, and throw a 404 error / refuse to bind if it's not there.. I know it's not much, but it might lead you in the right way :)
P.S. An example model binder + how it works can be found here:
http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx
Upvotes: 1
Reputation: 196
public ActionResult Index(int? id)
{
if(!id.HasValue())
{
throw new HttpException(404, "Are you sure you're in the right place?");
}
}
Upvotes: 8