简体   繁体   English

与MVC中的路由相关的问题

[英]Issue related to routing in MVC

I have MVC webapplication. 我有MVC Web应用程序。 i have written following rules in Route.config file 我在Route.config文件中编写了以下规则

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
            routes.MapRoute(
              name: "about",
              url: "Home/About-WDI",
              defaults: new { controller = "Home", action = "About"}
            );


        }

and in global.asax file, i have written following code: 在global.asax文件中,我编写了以下代码:

RouteConfig.RegisterRoutes(RouteTable.Routes);

now problem is when i type http://localhost:60123/Home/About-WDI it gives me error saying Resources not found 现在的问题是,当我键入http://localhost:60123/Home/About-WDI时,出现错误,提示我Resources not found

what is issue in above code? 上面的代码有什么问题? Thanks 谢谢

As already mentioned in the comments, the order of the routes is taken into concern. 如评论中已经提到的,要考虑路线的顺序。 And as your {id} parameter is optional the URL will be matched by the first route. 并且由于您的{id}参数是可选的,因此该网址将与第一条路由匹配。 So simply exchange both routes: 因此,只需交换两条路线:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
              name: "about",
              url: "Home/About-WDI",
              defaults: new { controller = "Home", action = "About"}
            );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM