views:

51

answers:

2

I'm seriously going crazy with this.

Here is what is in my Global.asax

routes.MapRoute("BlogDetails", "Blogs/{id}/{title}", new { controller = "Blogs", action = "Details", id = "" });
routes.MapRoute(
"Default",                                              // Route name
"{controller}/{action}/{id}",                           // URL with parameters
new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

Those are the only two routes I have.

When I try to access

http://mysite/Blogs/Edit/1 it doesn't work I get this error

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'mysite.Controllers.BlogsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

Why does this keep happening?

Thanks

I should also add my controller code looks like this

//
// GET: /Blogs/Edit/5

[Authorize]
public ActionResult Edit(int id)
{
  // do a bunch of stuff and return something
}
+5  A: 

Try the following

routes.MapRoute("BlogDetails", "Blogs/{id}/{title}",
 new { controller = "Blogs", action = "Details"},
 new { id = @"\d+" });

As far as MVC routes are concerned, {id} could be anything (even strings), so it matches Edit as a string, which can't go into the int id of your action.

Adding new { id= @"\d+" } as an extra parameter tells the routing system to only match numbers.

http://www.asp.net/(S(pdfrohu0ajmwt445fanvj2r3))/learn/mvc/tutorial-24-cs.aspx

Baddie
Holly crap it worked! Thank you!!!!!!!
hanesjw
+1  A: 

The 2 routes you are having are actually "confilicting" and in your case the first route gets picked instead of 2nd as you expect.

Maybe you need to modify your routes to remove the ambiguity:

routes.MapRoute("BlogDetails", "Blogs/{id}-{title}", new { controller = "Blogs", action = "Details", id = "" });
routes.MapRoute(
  "Default",                                              // Route name
  "{controller}/{action}/{id}",                           // URL with parameters
  new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

This definition will clearly distinguish between: http://mysite/Blogs/Edit/1 and http://mysite/Blogs/1-first

or as Baddie mentioned try to add constraint to the route.

Dmytrii Nagirniak