ASP.NET MVC 3 路由问题

发布于 2024-11-06 10:45:26 字数 1190 浏览 7 评论 0原文

我正在开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场春暖 2024-11-13 10:45:26

URL Forum/Topic/Create 将命中路由 Forum/Topic/{id}/{title}

问题是,路由 Forum/Topic/{ id}/{title}Forum/{controller}/{action}/{id} 基本上无法区分(您的路由引擎如何知道“Create”不是 <代码>id 为主题路线?

因此,我不知道有什么比用自己的路线声明每个操作更好的方法了:

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

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" });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文