Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

asp.net mvc - Make URL language-specific (via routes)

Its hard to find an answer to this specific question, so ill pop it up here:

We want to build up our urls completely language specific, meaning

www.mysite.com/EN

www.mysite.com/DE

this is done in the RouteConfig with

url: "{language}/{controller}/{action}/{id}"

But then the tricky part:

www.mysite.com/EN/Categories

www.mysite.com/DE/Kategorien

I.e. make the controllername appear in a multiple languages, but point to the same controller. Is this even possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, this is partially possible (the language part only). The controller name in a different language is definitely an interesting point, but I think it would be hard to achieve that. Think about what it would look like for Bidi languages like Arabic and Hebrew. It would probably be a good idea to use controller in different languages but you would create a havoc for yourself and I believe that you would have to change the underlying MVC structure to allow for this.

The language changing part is easy and could be done like below.

What you might want to look at is the globalization. Basically the language part corresponds to the current threads UI culture. What you need is the following:

  1. Define a route, something like this:

    var lang = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
    
    routes.MapRoute(
        name: "Default",
        url: "{language}/{controller}/{action}/{id}",
        defaults: new { language = lang, controller = "Home", 
                        action = "Index", id = UrlParameter.Optional }
     );
    
  2. Register Application_AcquireRequestState and define it something like this:

    protected void Application_AcquireRequestState()
    {
        var routes = RouteTable.Routes;
    
        var httpContext = Request.RequestContext.HttpContext;
        if (httpContext == null) return;
    
        var routeData = routes.GetRouteData(httpContext);
    
        var language = routeData.Values["language"] as string;
        var cultureInfo = new CultureInfo(language);
    
        System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
        System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;
    }
    

    Although CurrentUICulture is what you need switch languages load information from a resource file, you should also change the CurrentCulture to the same CultureInfo;

  3. Last, make sure you have corresponding resource files and a fallback resource file.

I used Name property of CultureInfo so you German would be de-DE, English en-US, etc. This should do the trick.

In case you need more information I could upload a sample MVC project for you to examine.

UPDATE: One crude way of doing what you want is to invert the order of route segments to something like this:

routes.MapRoute(
    name: "NewDefault",
    url: "{language}/{id}/{action}/{controller}",
    defaults: new { language = lang, controller = "Home", action = "Index", id = "Category"}
);

This way you could make a following request http://www.your_url.com/de/Kategorien. In this case Kategorien maps to the id rather than a controller. You controller stays in English or German (depending how you named it) but user sees a different language. In the background your view might be something like this:

public ActionResult Index(string id, string categoryName)
{
    // Fetch data based on category name (categoryName)
    return View();
}

You could pass additional information as parameters, but you will need to adjust your route to look something like: {language}/{category}/{subcategory}/{action}/{controller}

Just know that this can become pain in the neck in the long run and if you try this, then make sure you document it well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...