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

Posted

技术标签:

【中文标题】是否可以在不同的 AppDomain 中运行 ASP.NET MVC 路由?【英文标题】:Is it possible to run ASP.NET MVC routes in different AppDomains? 【发布时间】:2009-01-18 18:19:36 【问题描述】:

我在想出以下解决方案时遇到问题。我有一个最近从 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");

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

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

【问题讨论】:

【参考方案1】:

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

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

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

【讨论】:

是的,我一直在考虑这样做,这确实是一个很好的解决方案。由于我无法访问此网站的 IIS,因此必须询问我的网络主机。 并且仍然想知道是否可以从代码中添加 AppDomain。如果有 20 种语言呢?然后,您需要将它们全部添加为 IIS 应用程序,如果您想对它们进行更改,将会有很多管理工作。 嗯,另一种解决方案是添加名称包含文化代码(如“BlogPost#sv-SE”)的所有路线(无论哪种语言),然后使用当前的文化代码确定生成链接时要渲染的内容。不过,您必须编写自己的链接和 URL 帮助程序。 我想我只是向我的网络主机发送一封邮件,并要求他们添加另一个 IIS 应用程序,就像你最初建议的那样。随着 ASP.NET MVC 的进一步发展,将来有人可能会有添加多个路由表或自定义 URL 助手的解决方案。感谢您对此的帮助。

以上是关于是否可以在不同的 AppDomain 中运行 ASP.NET MVC 路由?的主要内容,如果未能解决你的问题,请参考以下文章