ASP.NET MVC 3 路由问题
我正在开发 http://mvcforum.codeplex.com 项目。
我们有 2 个区域,论坛和论坛管理。
我有一些命名路由,可以创建一个漂亮的 URL,并在 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" }
);
所以这几乎按预期工作。当我只是浏览论坛时,一切正常,但是当我需要发布主题(在主题控制器上创建方法)时,它会失败:
参数字典包含一个 参数“id”的条目为空 不可为空类型“System.Int32” 方法'System.Web.Mvc.ActionResult 索引(Int32,System.String,Int32)' in 'mvcForum.Web.Areas.Forum.Controllers.ForumController'。 可选参数必须是 引用类型、可为 null 的类型,或者是 声明为可选参数。 参数名称:参数
或多或少可以归结为不点击 Create 方法,而是选择 Index 方法。
知道我做错了什么吗?我应该/不需要哪些路线才能使其正常工作?
提前致谢! 斯蒂恩
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
URL
Forum/Topic/Create
将命中路由Forum/Topic/{id}/{title}
问题是,路由
Forum/Topic/{ id}/{title}
和Forum/{controller}/{action}/{id}
基本上无法区分(您的路由引擎如何知道“Create”不是 <代码>id 为主题路线?因此,我不知道有什么比用自己的路线声明每个操作更好的方法了:
The URL
Forum/Topic/Create
would hit the routeForum/Topic/{id}/{title}
The problem is, the route
Forum/Topic/{id}/{title}
andForum/{controller}/{action}/{id}
are mostly indistinguishable (how does your route engine know that "Create" isn't anid
for the Topic route?As such, I don't know any better way than declaring each action with their own route: