Problem with route routing Asp.net mvc

Asked

Viewed 212 times

1

I have the following route, where I get the representative’s id: http://localhost:18568/Representative/Index/1

So it’s working, but I’d like to show the representative’s name http://localhost:18568/representative’s name /Index/Jsblabla

in my Maproute I am 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 }
            );
        }

1 answer

0

To work exactly that: http://localhost:18568/representative-name/Index/1234, it would have to be the way below, but will have problems.

The parts of the route:

{representative} / {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 name of the representative you want to call another Controller other than the RepresentanteController will not succeed as the route will assume that the first parameter after the representative’s name is the action.

The above route does not work with: http://localhost/Jose/requests/list 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 }
            );

Ai works: http://localhost/Jose/requests/list or http://localhost/Jose/requests/detail/10

But also if you want to access the Overcontroller controller in http://localhost/About will not succeed, as it will be assumed that "about" is the name of the representative, sending to the controller Representative, then would have to create specific routes as the below, and this route below should come before the representative’s route:

routes.MapRoute(
                  name: "Sobre",
                  url: "sobre",
                  defaults: new { controller = "Sobre", action = "Index" }
                );
  • Murilo, I can’t understand how this would go employee, I took a test is nothing changed

  • I’m gonna fix something I screwed up and it’s gonna work. Keep an eye out

Browser other questions tagged

You are not signed in. Login or sign up in order to post.