3

How to define route that it link to default action.

For example,

/Customer/acme-company

/Client/bill-johnson

Always looking for method Get in controllers Client and Customer.

2 Answers 2

8

Use the parameter defaults.

routes.MapRoute(
    "MyRoute",                                   // Route name
    "{controller}/{someParameter}",              // URL with parameters
    new { controller = "Home", action = "Get" }  // Parameter defaults
);
Sign up to request clarification or add additional context in comments.

1 Comment

Like my comment on UpTheCreek's answer, this is valid as well. However, I think that depending on the scale and how the controllers are instantiated, it depends.
0

Something like this:

routes.MapRoute("Client",
    "Client/{name}",
    new { controller = "Client", action = "Get", });

5 Comments

then the second URL won't match...the first part of the route needs to be dynamic.
In my opinion it would be better to define two routes, rather than one to fit all situations.
Fair enough - matter of opinion i guess. But it looks like he's only wanting to server 2 urls, not really "all situations".
@RPN1984 & @UpTheCreek: You just have to add the second route (the answer is incomplete without the Customer route). Both approaches are valid, depending on the context. If you were custom injecting controllers through DI, for example, one route to rule them all is probably a bad idea.
@casparOne - i agree. But we can only go on what the OP has asked - which is a way to handle 2 routes. We have no idea what other routes or DI configuration he has setup.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.