0

我有这条路线:

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

            routes.MapRoute(
                "Funcionario",
                "{funcionario}",
                new { controller = "Funcionario", action = "Index", funcionario = UrlParameter.Optional },
                new string[] { "Route.Controllers" }
            );

            routes.MapRoute(
                "Servico",
                "{funcionario}/{servico}",
                new { controller = "Funcionario", action = "Servico", funcionario = UrlParameter.Optional, servico = UrlParameter.Optional },
                new string[] { "Route.Controllers" }
            );

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = @"\d+" }, // Parameter defaults
                new string[] { "Route.Controllers" }
            );            
        }

但我无法访问我的主页/索引。例如:

在职的

http://mydomain.com/pablo

http://mydomain.com/pablo/cozinha

http://mydomain.com/home/index/0

不工作

http://mydomain.com/

http://mydomain.com/home/index

4

1 回答 1

0

所有定义的路由都是通用的。

  1. http://mydomain.com/pablo将匹配第一个路由

  2. http://mydomain.com/pablo/cozinha将匹配第二条路线

  3. http://mydomain.com/将匹配第一个路由(所以主页不会显示)

  4. http://mydomain.com/home/index将匹配第二条路由。(所以主页不会显示)

路线应该按照从特定到通用的顺序来定义。例如,您可以将路线更改为,

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

        routes.MapRoute(
            "Servico",
            "Func/{funcionario}/{servico}",
            new { controller = "Funcionario", action = "Servico", funcionario = UrlParameter.Optional, servico = UrlParameter.Optional },
            new string[] { "Route.Controllers" }
        );

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = @"\d+" }, // Parameter defaults
            new string[] { "Route.Controllers" }
        );            
    }

所以,你的要求会像,

  1. http://mydomain.com/Func/pablo
  2. http://mydomain.com/Func/pablo/cozinha
  3. http://mydomain.com/
  4. http://mydomain.com/home/index

因此,应该仔细声明路由,以便请求不会匹配任何不需要的路由

于 2013-07-10T11:58:45.037 回答