【问题标题】:ASP.Net's String.Replace behaves differently if run on Windows 7 workstation than on Windows 2008 R2 server如果在 Windows 7 工作站上运行 ASP.Net 的 String.Replace 行为不同于在 Windows 2008 R2 服务器上运行
【发布时间】:2013-05-01 17:05:21
【问题描述】:

我有一个使用 .Net 框架 4.0 的 ASP.Net 项目。如果我的项目发布到我的 Windows 7 工作站,以下行可以正常工作:

strTemplate = strTemplate.Replace("<span style=\"background-color: yellow;\">", "");

但是,如果我将项目发布到 Windows 2008 R2 服务器,则不会发生上述替换。没有错误;服务器只是找不到模式并且不会替换它。谁能告诉我为什么,或者如何解决这个问题?我试过在我的模式字符串前面放一个“@”,但是无论反斜杠是否存在,字符串都想在“background-color”之前的双引号处终止。

【问题讨论】:

  • 很可能是因为strTemplate 不包含您正在搜索的字符串 - 所以请发布导致问题的变量的示例值。
  • 怀疑这是问题所在(并且只是评论),但我认为它将使用当前文化的字符串比较,它们可能会有所不同。
  • @Alexei - 没有 strTemplate 包含我正在搜索的字符串。如果项目在 Windows 7 机器上运行,则匹配;如果项目从 Windows 2008 R2 服务器运行,则不匹配
  • @Blam - 你能详细说明我如何检查服务器上的当前文化吗?
  • 表现出一些努力。在 msdn 中搜索 .NET Current Culture

标签: c# asp.net .net string replace


【解决方案1】:

试试这个:

strTemplate = strTemplate.Replace(@"<span style=""background-color: yellow;"">", "");

使用@ 表示法时,您必须加上两个双引号。

编辑: 假设您可以访问 Windows 2008 R2 服务器上正在运行的应用程序,请尝试在该服务器和您的 Windows 7 工作站上运行此代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Globalization;

namespace CurrentCulture
{
    public class Info : MarshalByRefObject
    {
        public void ShowCurrentCulture()
        {
            Console.WriteLine("Culture of {0} in application domain {1}: {2}",
                              Thread.CurrentThread.Name,
                              AppDomain.CurrentDomain.FriendlyName,
                              CultureInfo.CurrentCulture.Name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Info inf = new Info();
            // Set the current culture to Dutch (Netherlands).
            Thread.CurrentThread.Name = "MainThread";
            Thread.CurrentThread.CurrentCulture = CultureInfo.CurrentCulture;
            inf.ShowCurrentCulture();
            Console.Read();
        }
    }
}

如果您的 Windows 7 工作站上的当前文化与您的 Windows 2008 R2 服务器不同,您将需要调整您的 Windows 2008 R2 线程的当前文化以符合您的预期。

您可以将线程的文化设置为这样的新文化:

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("nl-NL");

我的代码是从MSDN页面修改的:http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture(v=vs.110).aspx

【讨论】:

  • 谢谢,Cameron,但这并没有给我任何不同的结果。搜索字符串仍然不匹配。
  • 按照 Blam 的建议,我已更新我的答案以包含 CurrentCulture 信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-08
  • 1970-01-01
  • 2011-12-10
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
  • 2011-01-17
相关资源
最近更新 更多