是否可以在不同的AppDomain中运行ASP.NET MVC路由?

时间:2009-01-18 18:19:37

标签: asp.net-mvc url-routing appdomain global-asax

我在考虑以下问题时遇到问题。我有一个博客,我最近从Web表单升级到MVC。该博客在两个不同的域上都可以使用瑞典语和英语,并且在IIS中的同一个网站上运行。

问题是我想在这两个网站上使用特定语言的网址,例如:

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

瑞典语:http://codeodyssey.se/arkiv/2009/1/15/code-odyssey-nasta-kapitel

目前我通过在每个请求上注册RouteTable来实现此功能,具体取决于调用的域。我的Global.asax看起来像这样(不是整个代码):

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()
{

}

目前这是有效的,但我知道这不是一个很好的解决方案。我已经得到一个“已经添加了项目。键入字典”错误,当说明这个应用程序时它似乎不稳定。

我只想在Application_Start中设置我的路由,而不是像我现在那样在每个请求上清除它们。问题是请求对象不存在,我无法知道应该注册哪种语言特定的路由。

一直在阅读AppDomain,但找不到很多关于如何在网站上使用它的例子。我一直在想这样的事情:

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

然后注册每个应用程序域中的每个网站路由,并根据网址将请求发送给其中一个网站。找不到有关如何以这种方式使用AppDomains的任何示例。

我完全偏离了轨道吗?或者有更好的解决方案吗?

1 个答案:

答案 0 :(得分:3)

ASP.Net运行时为您管理AppDomains,因此在您的代码中创建AppDomain可能不是一个好主意。

但是,如果可以,我建议创建多个IIS应用程序(一个用于http://codeodyssey.com,另一个用于http://codeodyssey.se)。将两个应用程序指向磁盘上的同一目录。这将为您提供您正在寻找的两个AppDomain。

然后,在Application_Start代码中,您可以检查域并相应地构建路由。

相关问题