ASP.Net/MVC项目无法识别/与系统区域设置冲突 [英] ASP.Net/MVC project not recognizing/conflicting with system Regional Settings

查看:79
本文介绍了ASP.Net/MVC项目无法识别/与系统区域设置冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我有一个真正的心灵弯曲。



我正在尝试建立一个新的机器环境,以便能够为我们的一个客户处理项目。我们这样做的原因是我们最终将把它交给他们的团队进一步发展,我们需要向他们提供如何设置和开始他们的指示。



当然,我正在设置的这个环境必须能够运行我们在TFS中拥有的这个客户端的所有项目。设置和测试一直很顺利,直到我们遇到一个问题(FormatException)的特定项目时,我们在将值转换为Decimal时使用了小数点字符(它想要一个逗号而不是一个点。)我们已经完成了机器上的区域设置和Regedit,但仍然没有运气。此计算机上的当前设置已配置为与项目运行正常的QA服务器的设置相匹配。它也可以在现有机器上顺利运行。另外,我创建了一个控制台应用程序,它将点识别为小数点,并在使用逗号时抛出FormatException。换句话说,它执行客户端项目应该在这台新机器上执行的操作。





这是抛出FormatException的代码:



Hi everyone. I have a real mind-bender here.

I'm currently trying to set up a new machine environment to be able to work on projects for one of our clients. The reason for us doing so is that we will eventually be handing it over to their team to develop further, and we need to provide them with instructions on how to set up and get going on their end.

Naturally, this environment I'm setting up must be able to run all the projects for this client, which we have in TFS. Setup and testing was going fine until we hit one particular project that is having an issue (FormatException) with the decimal point character we are using when converting a value to Decimal (it wants a comma instead of a dot.) We've gone through Region settings on the machine and Regedit, but still no luck. The current settings on this machine have been configured to match those of the QA server, where the project runs fine. It runs without hitch on existing machines as well. Also, I created a console app that recognises the dot as a decimal point and throws a FormatException when using a comma. In other words, it does what the client project should be doing on this new machine.


Here is the code that throws the FormatException:

//note: value is the string that is used
 public class DecimalModelBinder : DefaultModelBinder
    {
        #region Implementation of IModelBinder

        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            if(valueProviderResult.AttemptedValue.Equals("N.aN") ||
                valueProviderResult.AttemptedValue.Equals("NaN") ||
                valueProviderResult.AttemptedValue.Equals("Infini.ty") ||
                valueProviderResult.AttemptedValue.Equals("Infinity") ||
                string.IsNullOrEmpty(valueProviderResult.AttemptedValue))
                return 0m;

            var value = valueProviderResult.AttemptedValue.Replace(" ", "").Replace(",", "");
//The error is thrown here
            return valueProviderResult == null ? base.BindModel(controllerContext, bindingContext) : Convert.ToDecimal(value);
        }

        #endregion
    }

推荐答案

我们似乎已经通过使用以下代码来解析原始问题来处理将值解析为Decimal:



We seem to have sorted out the original issue by using the following code to handle parsing the value to Decimal:

if (valueProviderResult == null)
            {
                return base.BindModel(controllerContext, bindingContext);
            }
            else
            {
                decimal tmpValue;
                if (decimal.TryParse(value, System.Globalization.NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out tmpValue))
                {
                    return tmpValue;
                }
                else
                {
                    return 0m;
                }
            }





我们现在的问题是项目似乎没有恢复DateTime设置。在项目中运行DateTime.Now时,它会检测带有破折号而不是斜杠的日期,这是系统的日期格式。我已经看了IIS全局化设置,它们都设置为Invariant Culture



The issue we now have is that the project doesn't seem to be picking up the DateTime settings. When running DateTime.Now in the project, it detects a date with dashes instead of slashes, which is the system's date format. I've taken a look in the IIS Globalization settings, and they are all set to Invariant Culture


这篇关于ASP.Net/MVC项目无法识别/与系统区域设置冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆