本文介绍了如何使用Asp.net的Web API特定的CultureInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近添加的Web API到现有的VS桌面应用程序,一切工作正常,直到昨天,当我不得不添加了三个参数,其中之一日期的GET方法。
嗯,一开始我以为这将是小菜一碟,但出乎我的意料,我注意到,当我发2014年7月9日(第9 7月),其中应用程序安装它像2014处理服务器上/ 09/07(9月7日),因为这个原因,我所有的比较从来没有工作过。

I've recently added WEB API to an existing VS desktop application and everything worked fine, until yesterday when I had to add a GET method that took three parameters, one of them a Date.Well, at first I thought that that was going to be a piece of cake, but much to my surprise I noticed that when I sent 2014/07/09 (9th July) on the server where the application was installed it was treated like 2014/09/07 (7th September) and for that reason all my comparisons never worked.

我试图像一个GET方法更改为POST方法,改变我的区域和语言选项设置为相同的服务器上,传递的日期字符串中使用的部分服务器上创建一个DateTime对象的事串。遗憾的是他们没有工作。

I have tried things like changing from a GET method to a POST method, changing my Regional and Language Options settings to the same on the server, passing the date as a String a created a Datetime object on the server using the parts of the string. Unfortunately none of them worked.

然后我记得这个桌面应用程序对其WCF项目的一些方法(我现在传递给网络API),它没有问题,通过日期都没有。展望在code一会我发现,他们用这样的事情在每个班级的WCF他们使用日期的项目:

Then I remember that this desktop application have some methods on its WCF project (which I'm passing now to web API) that passed dates with no problem at all. Looking in the code for a while I found that they used something like this on every class of they WCF project that uses dates:

Imports System.Globalization
Imports System.Security.Permissions
Imports System.Threading

Public Class ServicioRemotoVentas
    Implements IServicioRemotoVentas        
    Public Sub New()
        MyBase.New()
        Thread.CurrentThread.CurrentCulture = New CultureInfo("es-PE", False)
    End Sub

这肯定 Thread.CurrentThread.CurrentCulture =新的CultureInfo(ES-PE,FALSE),必须有什么东西。
现在,我想知道,如果你以前使用过类似的东西在网络API?如果是的话怎么和你在哪里把这样的配置。

Surely this Thread.CurrentThread.CurrentCulture = New CultureInfo("es-PE", False), must be there for something.Now I would like to know if you have used something like that in Web API before? if so how and where did you put such a configuration.

这是在我的电脑中的设置:

These are the settings on my pc :

而这些服务器设置:


我差点忘了提,我通过使用此格式的所有日期的 YYYY / M / D 使用JSON所有其它参数。
它是也许,当串在Web API中进行反序列化这是使用在系统日期格式完成,因为我没有指定文化信息的使用?或者它是一个JSON错误试图序列化时/反序列化的日期?

I almost forgot to mention that I pass all the dates using this format yyyy/M/d with all the other parameters using json.Is it perhaps that when the string is deserialized in the Web API this is done using the system date format because I haven't specify the culture info to use?? or maybe it is a Json error when trying serialize/deserialize the dates??

与往常一样,您可以提供的任何意见或资源将大大AP preciated。

As always, any advice or resources you could provide would be greatly appreciated.

推荐答案

正如在评论中讨论的,ASP.NET运行时确实有这些情况的解决方案:它是 web.cofig 元素的 - (请参阅MSDN的)

As discussed in the comments, the ASP.NET runtime does have a solution for these scenarios: it is the web.cofig element <globalization> - (see MSDN <globalization> Element)

它的结构被定义为:

<configuration>
 <system.web>
  <globalization 
    enableClientBasedCulture="true|false"
    requestEncoding="any valid encoding string"
    responseEncoding="any valid encoding string"
    fileEncoding="any valid encoding string"

    responseHeaderEncoding = "any valid encoding string" 
    resourceProviderFactoryType = string
    enableBestFitResponseEncoding = "true|false"

    culture="any valid culture string"
    uiCulture="any valid culture string"/>

所以,在情况下,我们要强制服务器的/ dev工作站的en-US 行动文化,我们应该使用这些明确的设置:

So, in case, that we want to force server/dev workstation to act in en-US culture we should use these explicit settings:

<globalization 
    enableClientBasedCulture="false" 
    uiCulture="en-US" 
    culture="en-US" />

这将使用适当的(期望和设置)文化为任何HTTP请求。​​

This will use the proper (desired and set) culture for any http request.

另外有趣的可能是在默认设置概述:

Also interesting could be the default setting overview:

<globalization 
    requestEncoding="utf-8" 
    responseEncoding="utf-8" 
    fileEncoding="" 
    culture="" 
    uiCulture="" 
    enableClientBasedCulture="false" 
    responseHeaderEncoding="utf-8" 
    resourceProviderFactoryType="" 
    enableBestFitResponseEncoding="false" />

又见类似的位置:

See also similar here:




  • 和本身

  • Set uiCulture automatically based on browser accept language
  • How to change my computer's cultureInfo
  • and the globalization Element itself

这篇关于如何使用Asp.net的Web API特定的CultureInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 09:18