0

我有一个名为 PartialController 的控制器,并有一个名为 AboutMe 的操作方法。我可以从 localhost/Partial/AboutMe 访问我的操作方法。但我想从 localhost/About 访问它。所以我改变了我的路线:

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

        routes.MapRoute(
        name: "About",
        url: "About",
        defaults: new
            {
                controller = "Partial",
                action = "AboutMe",
                id = UrlParameter.Optional
            }
        );

但是当我尝试从 localhost/About 访问 AboutMe 操作时出现 404 异常。需要建议。

4

1 回答 1

1

定义你的About Route第一个。当涉及到路由时,顺序很重要,它们按照定义的顺序应用。

  routes.MapRoute(
        name: "About",
        url: "About",
        defaults: new
            {
                controller = "Partial",
                action = "AboutMe",
                id = UrlParameter.Optional
            }
        );

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Blog", action = "Index", id =  UrlParameter.Optional }
        );
于 2013-11-02T23:35:19.377 回答