2

I'm working on the http://mvcforum.codeplex.com project.

We have 2 areas, Forum and ForumAdmin.

I have a few named routes, to make a nice URL with forum/topic titles in the URL:

context.MapRoute("ShowTopic", "Forum/Topic/{id}/{title}", new { controller = "Topic", action = "Index" });
context.MapRoute("ShowForum", "Forum/Forum/{id}/{title}", new { controller = "Forum", action = "Index" });
context.MapRoute("ShowCategory", "Forum/Category/{id}/{title}", new { controller = "Category", action = "Index" });

context.MapRoute(
    "Forum_default",
    "Forum/{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new string[] { "mvcForum.Web.Areas.Forum.Controllers" }
);

So this almost works as intended. When I'm just browsing the forum everything works fine, but when I need to post a topic (Create method on the Topic controller), it fails:

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32, System.String, Int32)' in 'mvcForum.Web.Areas.Forum.Controllers.ForumController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

Which more or less boils down to not hitting the Create method, but selecting the Index method.

Any idea what it is I'm doing wrong? And what routes I should have/not have to get this working?

Thanks in advance! Steen

tereško
  • 58,060
  • 25
  • 98
  • 150
Steen Tøttrup
  • 3,755
  • 2
  • 22
  • 36

1 Answers1

4

The URL Forum/Topic/Create would hit the route Forum/Topic/{id}/{title}

The problem is, the route Forum/Topic/{id}/{title} and Forum/{controller}/{action}/{id} are mostly indistinguishable (how does your route engine know that "Create" isn't an id for the Topic route?

As such, I don't know any better way than declaring each action with their own route:

context.MapRoute("CreateTopic", "Forum/Topic/Create/", 
    new { controller = "Topic", action = "Create" });
context.MapRoute("ShowTopic", "Forum/Topic/{id}/{title}", 
    new { controller = "Topic", action = "Index" });
Matt Mitchell
  • 40,943
  • 35
  • 118
  • 185
  • This is what I was a bit afraid of, that way I have to create named routes for every action there is. – Steen Tøttrup May 10 '11 at 08:02
  • The real problem is that the "ShowTopic" route has "Topic" as the 2. part of the URL, which in this case matches an actual Controller. For now I guess the fix will be to rewrite the "ShowTopic" route to something that does not have Topic as the 2. part. – Steen Tøttrup May 11 '11 at 07:39