ASP MVC Route Config - 无法找到资源错误 [英] ASP MVC Route Config - The resource cannot be found error

查看:127
本文介绍了ASP MVC Route Config - 无法找到资源错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是asp mvc的新手,目前,我的演示项目结构如下:



I'm newbie in asp mvc, currently, my demo project structure like this:

Areas -- Comment -- Controller -- HomeController
                               -- ManageController




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



当我浏览网址时:


When I browsing url like :

http://localhost/Comment/Manage/ --> return view successfully
http://localhost/Comment/PostMsg --> error "The resource cannot be found."



任何人都知道为什么asp mvc无法解析我的控制器: - (

这是我的global.asax.cs路由配置:


Anyone have any idea why asp mvc doesn't resolve my controller :-(
here is my global.asax.cs route config:

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



这里是我的区域注册路线配置:


here is my area registration route config:

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的行动

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



ISSUE RESOLVED ,编辑区域注册路线配置(解决方法):




Problem : Comment/PostMsg url was resolved as an Controller in Comment Area

Goal : Comment/PostMsg url was resolved as an Action of CommentController
Any help would be appreciated :-)

ISSUE RESOLVED, edit area registration route config (work around):

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

推荐答案

这是我的示例代码正常工作

this is my example code thats work properly
     public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
     "viewstudent", // Route name
     "view/viewstudent/{stdid}", // URL with parameters
     new { controller = "Movie", action = "SelectById", id = UrlParameter.Optional } // Parameter defaults
     );

            routes.MapRoute(
       "Default", // Route name
       "{controller}/{action}/{id}", // URL with parameters
       new { controller = "Home", action = "Index", id = UrlParameter.Optional }// Parameter defaults
);
                
    
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
         
        RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);  
        
        }


编辑区域注册路线配置(解决方法):



Edit area registration route config (work around):

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


转到Web项目的属性页面,然后选择Web选项卡。在开始操作部分中,将其设置为特定页面,但将文本框保留为空。
Go to the properties page of the Web project and select the Web tab. In the Start Action section, set it to Specific Page, but leave the textbox empty.


这篇关于ASP MVC Route Config - 无法找到资源错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆