ASP MVC路由配置 - 找不到资源错误



我是ASP MVC中的新手,目前是我的演示项目结构:

Areas -- Comment -- Controller -- HomeController
                               -- ManageController
Controller -- HomeController
          |-- CommentController
                 |____ PostMsg
                 |____ DeleteMsg
Views -- Home
     |     |--- Index.cshtml
     |-- Comment
           |--- PostMsg.cshtml
           |--- DeleteMsg.cshtml

当我浏览URL喜欢:

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已作为注释区域的控制器解决

目标:评论/Postmsg URL被解决为注释controller的动作

任何帮助将不胜感激: - )

问题已解决,编辑区域注册路由config(工作):

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

您是否有操作 index ** in ** postmsgcontroller demo.web.areas.comment.controllers >> demo.web.areas ?据我了解,您没有

更新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" }
);

最新更新