【问题标题】:Possible Bug - Asp.net Routing Localization strange problem with Thread可能的错误 - 线程的 Asp.net 路由本地化奇怪问题
【发布时间】:2011-08-18 12:56:29
【问题描述】:

我有两个页面 A 和 B。两者都应该使用适当的全局资源显示文本,具体取决于 Web.Conf 中的文化设置。

Page A 上,我能够根据文化设置(因此在本例中为法语)可视化文本。

Page A 也有一个链接,它使用GetUrl 创建到Page B 的 URL(路由)

<asp:HyperLink 
           runat="server" 
           Text='<%# Eval("Title") %>' 
           NavigateUrl='<%# GetRouteUrl("TopicHub", new {TitleUrl = Eval("TitleUrl")}) %>'>
</asp:HyperLink>

在访问页面 B 时,我无法再看到法语文字,并且应用了英语文本(默认值)。 奇怪的事情是,如果我直接在浏览器中打开页面 B,文字会以法语显示。

所以我调试了 Global.asax.cs,我发现在单击使用 GetRouteUrl 创建的链接时,var Thread.CurrentThread.CurrentCultureCurrentUICulture 是英文的,而不是我的 Web.Config 的法文。

我的问题是:

  • 这可能是什么问题?也许是一个错误?
  • 知道如何解决吗?

我的想法已经用完了,最后想到的是使用 Web.Conf 中的值在 Global.asax.cs 中强制 Thread.CurrentThread.CurrentCulture

请让我知道您对此的看法。谢谢!

A页:

<asp:Literal ID="uxLatestGuides" 
             runat="server" 
             Text="<%$ Resources:Global, LatestGuides %>" />

B页:

<asp:Literal ID="uxLatestGuides" 
             runat="server" 
             Text="<%$ Resources:Global, LatestGuides %>" />

Global.asax.cs

protected void Application_BeginRequest(object sender, EventArgs e)
{
  var check1 = Thread.CurrentThread.CurrentCulture;
  var check2 = Thread.CurrentThread.CurrentUICulture;
}

Web.Config

<?xml version="1.0"?>
    <configuration>
        <system.web>
            <pages enableViewState="false" theme="Cms-FE-00" >
            </pages>
            <globalization culture="fr" uiCulture="fr"/>
        </system.web>
    </configuration>

【问题讨论】:

    标签: c# asp.net routing


    【解决方案1】:

    我找到了解决这个问题的方法。

    分析 Global.asax.cs Routes 我注意到 virtual URL topic/{TitleUrl} 将映射到 physical Url ~/Cms/FrontEndCms/CategoryList.aspx

     routes.MapPageRoute("TopicHub", "topic/{TitleUrl}", "~/Cms/FrontEndCms/CategoryList.aspx");
    

    在我的特定情况下,我在~/Cms/FrontEndCms/ 中有 Web.Config 文件,因此当在 mysite.com/topic/my-page 之类的路由 URL 页面上创建链接时,Web.Config 将不在正确的位置,而是在另一个文件夹中所以我的文化设置不适用。

    解决方案是将 Web.Config 移至我网站的根目录,以便它适用于所有路由。希望可以帮助别人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-29
      相关资源
      最近更新 更多