mccalltd / AttributeRouting

Define your routes using attributes on actions in ASP.NET MVC and Web API.
http://mccalltd.github.io/AttributeRouting/
MIT License
416 stars 89 forks source link

Dynamic routing based on url. #169

Open Cefa68000 opened 11 years ago

Cefa68000 commented 11 years ago

I am wondering about a problem i have couple of websites in different languages same application different bindings. The domain ends with se, fi, dk

And i would like to have different translated urls for different sites. But that does not seem possible right now if i use the fluenttranslationprovider a url for se would work on fi site and dk and thats not what i want (duplicate content and stuff)

How would i go about doing something like that because i don't have access to the request.url on application start so i dont know which url to handle. And is it even possible to change translation provider on begin request event??

Do you have any solution to my particular problem?

mdmoura commented 11 years ago

Hello,

I will upload a localization project with AR to Github during this week.

There are still a few issues but it will be open for discussion.

Cheers, Miguel

mccalltd commented 11 years ago

Sorry for the huge delay. Here's some code that might work. I am still trying to understand the problem.

public static class AttributeRouting 
{
    public static void RegisterRoutes(RouteCollection routes) 
    {    
        // See http://github.com/mccalltd/AttributeRouting/wiki for more options.
        // To debug routes locally using the built in ASP.NET development server, go to /routes.axd

        routes.MapAttributeRoutes(config =>
        {
            config.AddRoutesFromAssemblyOf<DefaultController>();
            config.AddTranslationProvider(GetTranslationProvider());

            // Inbound request handling: 
            // - Don't honor URL's for other cultures.
            // - Determine the culture by taking the last section of the URL's host.
            // See: http://attributerouting.net/#localizing-urls
            config.ConstrainTranslatedRoutesByCurrentUICulture = true;
            config.CurrentUICultureResolver = 
                (httpContext, routeData) => httpContext.Request.Url.Host.Split('.').Last();
        });
    }

    // Outbound URL generation:
    // - Uses the CurrentUICulture of the executing thread.
    // - Set the culture by reflecting upon the last section of the URL's host.
    public static void SetCurrentUICulture(HttpRequest request)
    {
        var language = request.Url.Host.Split('.').Last();
        var cultureInfo = new CultureInfo(language);
        Thread.CurrentThread.CurrentUICulture = cultureInfo;
    }

    private static TranslationProviderBase GetTranslationProvider()
    {
        var provider = new FluentTranslationProvider();

        provider.AddTranslations()
                .ForController<DefaultController>()
                .RouteUrl(x => x.About(), new Dictionary<string, string>
                {
                    { "es", "About-es" },
                    { "fr", "About-fr" }
                });

        return provider;
    }

    public static void Start() 
    {
        RegisterRoutes(RouteTable.Routes);
    }
}

You'll also need to do this in your global.asax:

public MvcApplication()
{
    BeginRequest += (sender, args) => AttributeRouting.SetCurrentUICulture(Request);
}

The main points are:

If this doesn't help, please let me know.