【问题标题】:ASP MVC Route Config - The resource cannot be found errorASP MVC 路由配置 - 找不到资源错误
【发布时间】: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


    【解决方案1】:

    您在 Demo.Web.Areas.Comment.Controllers 的 **PostMsgController 中有操作 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" }
    );
    

    【讨论】:

    • 因为我有 PostMsg 操作 :-( 和 Views\Comment\ 文件夹中的 PostMsg.cshtml :-(
    • 对不起,在 Demo.Web.Areas.Comment.Controllers
    • 我不想在 Areas.Comment.Controller.HomeController 中推送 PostMsg 操作。我的目标是在 CommentController 中调用 PostMsg 操作 :-)
    • 您应该稍后注册您的区域!或制定差异规则
    • 是的,我的问题,asp mvc 将 PostMsg 检测为评论区域中的控制器 :-((我希望它检测 PostMsg 作为评论控制器的操作)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 2019-11-26
    • 2012-03-06
    • 1970-01-01
    相关资源
    最近更新 更多