ASP MVC Route Config - 无法找到资源错误

时间:2012-11-07 15:19:00

标签: asp.net-mvc asp.net-mvc-routing

我是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网址被解析为评论区域中的控制器

目标:评论/ PostMsg网址已被解析为CommentController的动作

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

已解决的问题,编辑区域注册路由配置(解决方法):

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

1 个答案:

答案 0 :(得分:3)

你有 Demo.Web.Areas.Comment.Controllers 的** PostMsgController 中的索引**吗?据我所知,你没有

更新1

从你的代码我thnik / 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" }
);