1

我是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" }
            );
        }
4

1 回答 1

3

你在 Demo.Web.Areas.Comment.Controllers 的**PostMsgController 中有 action Index **吗?据我了解,你没有

更新 1

从您的代码中我认为 /Comment/PostMsg - 可能是您在 Demo.Web.Areas.Comment.Controllers 中的控制器 PostMsgController 的操作索引

更新 2

比你应该做的

context.MapRoute(
    "Comment_default",
    "Comment/PostMsg",
    new { controller = "Comment", action = "PostMsg", id = UrlParameter.Optional },
    new[] { "Demo.Web.Controllers" }
);
于 2012-11-07T15:26:52.193 回答