How to pass more than one Controller option in Routes.Maproute()

Asked

Viewed 58 times

0

I have the following route:

 routes.MapRoute(
           "Contracts",
           "Home/Contract/{contract}",
           new
           {
               controller = "Home",
               action = "Contract",
               contract = UrlParameter.Optional
           },
           new { contract = @"\w+" }
        );

It is possible to configure this route to receive both the controller Home(as already configured), how much for a new controller calling for Contributor? Basically an OR within the parameter controller.

  • 1

    Why don’t you change the route to "{controller}/Contract/{contract}",?

  • @LINQ but is the parameter controller that is inside the method Routes.Maproute?

  • @LINQ worked.... I just didn’t understand why, if I am declaring the controller = "Home", action = "Contract". If you can explain to me why it worked even with this statement I would appreciate.

  • 2

    The description of the third parameter of the Maproute method(): "An object that contains default value routes". When you give a new { controller = "Home" } you are saying that if I do not pass any controller value in my url, it is for me to use a default value for that parameter, in which case it would be yours Home. If I pass some parameter with the controller value, I ignore this default value.

  • 1

    @Gabrielcoletta understood, thanks for the explanation, so just to see if I really understand... what counts even to set the route is the second parameter? in the case "Home/Contract/{contract}",?

  • That’s where Gabriel spoke. The first parameter is the name of the route, the second is the format of it, the third are the standards.

Show 1 more comment

1 answer

2


Just change the route statement to:

routes.MapRoute(
       "Contracts",
       "{controller}/Contract/{contract}",
       new
       {
           controller = "Home",
           action = "Contract",
           contract = UrlParameter.Optional
       },
       new { contract = @"\w+" }
    );

Browser other questions tagged

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