Jupaol
Jupaol

Reputation: 21365

MVC3 Routing strange behavior

I found something strange with routing...

I´m testing a MVC3 application in Visual Studio Web Express 2012

The action links look like:

<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>

Then this is what is happening:

If I remove my custom routing everything works as expected

Upvotes: 2

Views: 120

Answers (2)

Jupaol
Jupaol

Reputation: 21365

I just found the problem

Basically I read several routing articles and finally I got it, my problem was that my custom route was been picked up always after I clicked the About link

Why?

Let's consider it:

When my URL was http://localhost:54870/, my custom route was not picked up because I didn't have default values for {language} and {country} therefore my route didn't match

But when my URL was http://localhost:54870/Home/About my custom route was always picked up because the route engine assumed that Home/About were the {language} and {country} segments and since I had default values for {controller} and {action} the rout simply was a match

Well I learnt my lesson and I learnt more about routing. In the future I'm planning to follow the KISS principle when defining routes

Upvotes: 2

Display Name
Display Name

Reputation: 4732

Try replacing your route with something like this:

routes.MapRoute(
    "default_localization",
    "{language}/{country}/{controller}/{action}/{id}",
    new { language = "en", country = "US", controller = "Home", action = "Index", id = UrlParameter.Optional }
);

See if that works.

Hope this is of help to you.

Upvotes: 0

Related Questions