我是asp mvc的新手,目前,我的演示项目结构如下:
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 在评论区被解析为控制器
目标: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" }
);
}