Problem with route routing asp.net mvc

1

I have the following route, where I get the delegate id: link

So it's working, but I'd like to show the name of the rep link / Index / JsBlaBla

In my MapRoute I'm doing so unsuccessfully

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

            routes.MapRoute(
              name: "Representante",
              url: "nome-do-representante/Index/{chave}",
              defaults: new { controller = "Representante", action = "Index" },
              constraints: new { chave = @"^[a-zA-Z0-9\-\/_]{2,}$" }
            );



            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    
asked by anonymous 10.11.2016 / 17:22

1 answer

0

To work exactly this: link , it would have to be as below, but it will have problems.

The parts of the route:

{rep} / {action} / {key}

jose / Index / 1234

This route will always call the controller RepresentanteController

routes.MapRoute(
              name: "Representante",
              url: "{representante}/{action}/{chave}",
              defaults: new { controller = "Representante", action = "Index", chave = UrlParameter.Optional }
            );

But if after the delegate name you want to call another Controller other than RepresentanteController you will not be able to, because the route will assume that the first parameter after the delegate name is the action.

The above route does not work with: link because you will find that "requests" is a controller action RepresentanteController

The following way gets better:

routes.MapRoute(
              name: "Representante",
              url: "{representante}/{controller}/{action}/{chave}",
              defaults: new { controller = "Representante", action = "Index",  chave = UrlParameter.Optional }
            );

It works: link or link

But also if you want to access the Controller SobreController in link you will not be able to, because it will be assumed that "about" is the name of the representative, sending pro controller representative, then you would have to create specific routes like the one below, and this route below should come before the route of the representative:

routes.MapRoute(
                  name: "Sobre",
                  url: "sobre",
                  defaults: new { controller = "Sobre", action = "Index" }
                );
    
10.11.2016 / 18:03