views:

425

answers:

1

I am having problems with thinking up a solution for the following. I got a blog which I recently upgraded from web forms to MVC. The blog is avalible in both swedish and english on two different domains and are running in the same web site in IIS.

The problem is that I would like language specific urls on the both sites, like this:

English: http://codeodyssey.com/archive/2009/1/15/code-odyssey-the-next-chapter

Swedish: http://codeodyssey.se/arkiv/2009/1/15/code-odyssey-nasta-kapitel

At the moment I have made this to work by registering the RouteTable on each request depending on which domain is called. My Global.asax Looks something like this (not the whole code):

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    string archiveRoute = "archive";

    if (Thread.CurrentThread.CurrentUICulture.ToString() == "sv-SE")
    {
        archiveRoute = "arkiv";
    }

    routes.MapRoute(
        "BlogPost",
        archiveRoute+"/{year}/{month}/{day}/{slug}",
        new { controller = "Blog", action = "ArchiveBySlug" }
        );

    routes.MapRoute(
        "Default",                                              // Route name
        "{controller}/{action}/{id}",                           // URL with parameters
        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
    );

    routes.MapRoute(
        "404-PageNotFound",
        "{*url}",
        new { controller = "Error", action = "ResourceNotFound" }
    );

}

void Application_BeginRequest(object sender, EventArgs e)
{

    //Check whcih domian the request is made for, and store the Culture
    string currentCulture = HttpContext.Current.Request.Url.ToString().IndexOf("codeodyssey.se") != -1 ? "sv-SE" : "en-GB";

    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(currentCulture);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentCulture);

    RouteTable.Routes.Clear();

    RegisterRoutes(RouteTable.Routes);

    Bootstrapper.ConfigureStructureMap();

    ControllerBuilder.Current.SetControllerFactory(
        new CodeOdyssey.Web.Controllers.StructureMapControllerFactory()
        );
}

protected void Application_Start()
{

}

This works at the moment but I know it not a great solution. I have been getting a "Item has already been added. Key in dictionary" error when stating up this app and it does not seems stable at times.

I would like to only set up my routes in the Application_Start as they should and not having to clear them on every request like I am doing now. Problem is that the request object does not exist and I have no way of knowing which of the language specific routes I should register.

Been reading about the AppDomain but could not find many examples on how to use it on a web site. I'we been thinking to star something like this:

protected void Application_Start()
{
   AppDomain.CreateDomain("codeodyssey.se");
   AppDomain.CreateDomain("codeodyssey.com");
}

Then registring each web sites routes in each app domain and send the requests to one of them based on the url. Can't find any examples on how to work with AppDomains in this manner.

Am I completely off track? Or is there a better solution for this?

+2  A: 

The ASP.Net runtime manages AppDomains for you, so its probably not a good idea to create AppDomains in your code.

However, if you can, I would suggest creating multiple IIS Applications (one for http://codeodyssey.com and one for http://codeodyssey.se). Point both applications at the same directory on disk. This will give you the two AppDomains you are looking for.

Then, in your Application_Start code, you can check the domain and build routes accordingly.

anurse
Yes I have been thinking of doing this and that would indeed be a good solution. Have to ask my web host since I don't have access to the IIS for this web site.
jesperlind
And still wonder if it could be possible to add AppDomain from the code. What about if there were 20 languages? Then you would need to add them all as a IIS App and there would be much administration if you want to change something to all of them.
jesperlind
Well, one other solution is to add all the routes (no matter which language) with names that include the culture code (like "BlogPost#sv-SE") and then use the current culture code to determine which to render when generating links. You'd have to write your own Link and URL helpers though.
anurse
I'll think I just send a mail to my web host and ask them to add another IIS App, like you suggested at first. Someone will probably have a solution for adding multiple routing tables, or custom URL helpers in the future as ASP.NET MVC develops further.Thanks for your help with this.
jesperlind