2

I want to create multilingual URLs for my ASP.NET MVC 3 project.

Non-default language should be passed in URL as the first parameter using routing. Like /es/blog/some-blog-post-slug

English will be used as default language and require not to pass the language in URL. Like /blog/some-blog-post

I tried to do it with routing but either routing breaks or URL generation.

I tried many options with Routing, with custom routing. Currently I have:

routes.MapRoute(
"", // Route name
"{lang}/{controller}/{action}/{slug}", // URL with parameters
new { controller = "Test", action = "Index", slug = UrlParameter.Optional }, // Parameter defaults
new { lang = "^[a-z]{2}$" }
);

routes.MapRoute(
"", // Route name
"{controller}/{action}/{slug}", // URL with parameters
new { controller = "Test", action = "Index", slug = UrlParameter.Optional } // Parameter defaults
);

In my test view I have:

@Url.RouteUrl(new { controller = "Test", action = "Details", slug = "some-blog-post-slug" })
<br />
@Url.RouteUrl(new { lang = "es", controller = "Test", action = "Details", slug = "some-blog-post-slug" })

When I open the test view from "http://localhost:19038/test/details/my-blog-post-one" URL I see in my browser:

/Test/Details/some-blog-post-slug 

/es/Test/Details/some-blog-post-slug

That's pretty much what I need

But when I open the test view from "http://localhost:19038/es/test/details/my-blog-post-one" URL I see different URL generated:

/es/Test/Details/some-blog-post-slug 

/es/Test/Details/some-blog-post-slug

When I open "http://localhost:19038/en/test/details/my-blog-post-one" I get:

/en/Test/Details/some-blog-post-slug 

/es/Test/Details/some-blog-post-slug

And "http://localhost:19038/xx/test/details/my-blog-post-one" produces:

/xx/Test/Details/some-blog-post-slug 

/es/Test/Details/some-blog-post-slug

Why "xx" is appended? I don't pass a language to the Razor HTML URL helper. I also tried to use lang = "en" in controller's default parameters - it didn't help.

I could end up adding the language to all URLs, but I want the URLs with default ("en") language to omit the language in URL, and if even someone passing "en" - redirect to the URL without the language, and when URL is generated for "en" URL should not include it.

What is the right way to do such thing?

Thank you.

2 Answers 2

1

When you visit /xx/Test/Details/some-blog-post-slug ASP.NET MVC 3 adds {lang} with value xx to the route data, and that is taken into account when you invoke:

@Url.RouteUrl(new { controller = "Test", action = "Details", slug = "some-blog-post-slug" })

If you specify

@Url.RouteUrl(new { lang = (string)null, controller = "Test", action = "Details", slug = "some-blog-post-slug" })

Then it renders the value you expect, by using the alternate route with the missing lang resulting in:

/Test/Details/some-blog-post-slug
Sign up to request clarification or add additional context in comments.

1 Comment

I'm using this approach for now. Didn't find anything better yet.
0

What you might want is a Localisation attribute. See this post for details:

geekswithblogs.net/shaunxu/archive/2010/05/06/localization-in-asp.net-mvc-ndash-3-days-investigation-1-day.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.