Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top