【发布时间】:2012-10-27 16:19:55
【问题描述】:
我是asp mvc的新手,目前我的demo项目结构如下:
Areas -- Comment -- Controller -- HomeController
-- ManageController
Controller -- HomeController
|-- CommentController
|____ PostMsg
|____ DeleteMsg
Views -- Home
| |--- Index.cshtml
|-- Comment
|--- PostMsg.cshtml
|--- DeleteMsg.cshtml
当我浏览网址时:
http://localhost/Comment/Manage/ --> return view successfully
http://localhost/Comment/PostMsg --> error "The resource cannot be found."
任何人都知道为什么 asp mvc 不能解析我的控制器 :-(
这是我的 global.asax.cs 路由配置:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Demo.Web.Controllers" }
);
这是我的区域注册路线配置:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Comment_default",
"Comment/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Demo.Web.Areas.Comment.Controllers" }
);
}
问题:评论/PostMsg url 被解析为评论区的控制器
Goal : Comment/PostMsg url 被解析为 CommentController 的 Action
任何帮助将不胜感激:-)
问题已解决,编辑区域注册路线配置(解决方法):
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Comment_default",
"Comment/PostMsg",
new { controller = "Comment", action = "PostMsg", id = UrlParameter.Optional },
new[] { "Demo.Web.Controllers" }
);
context.MapRoute(
"Comment_default",
"Comment/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Demo.Web.Areas.Comment.Controllers" }
);
}
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-routing