1

私は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 がコメント エリアのコントローラーとして解決された

目標: Comment/PostMsg url は 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" }
            );
        }
4

1 に答える 1

3

do you have action Index ** in **PostMsgController of Demo.Web.Areas.Comment.Controllers? as i understand you havn't

UPDATE 1

from your code i thnik that /Comment/PostMsg - could be your action Index of controller PostMsgController in Demo.Web.Areas.Comment.Controllers

UPDATE 2

than you should do

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