ASP.NET MVC 3 route issues
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 开发者_StackOverflow社区 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
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" });
精彩评论