Servicestack POSTing DateTime issue - c#

Weirdly, this works locally but when it's deployed to an Azure website it doesn't
The POST variables that fail on Azure are:
name=Test&venue=1&fromDate=26%2F06%2F14&toDate=01%2F07%2F14&eventType=1
If I POST the following it works:
name=Test&venue=1&eventType=1
So it must be something to do with the date format.
The data type on the request is a DateTime, if I set this to string it works - I can then call request.ConvertTo<Model> and everything is passed over as expected. I just don't want to set the fromDate and toDate to be strings on the request.
Does anyone know why this would fail?

That looks like a globalization issue. The Azure servers will be using en-US culture that expects the dates in mm/dd/yy format for parsing.
But you are using UK date format dd/mm/yy, so the culture is en-GB. Your development machine will be set to use the en-GB locale already, hence no problems when testing locally.
You can specify the culture in your web.config:
<configuration>
<system.web>
<globalization uiCulture="en-GB" culture="en-GB" />
</system.web>
</configuration>
Or you can do so at runtime in global.asax:
public void PreRequestHandlerExecute(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
}
Or you could modify how ServiceStack.Text deserializes DateTime, but I wouldn't recommend that approach. In the AppHost Configure method:
JsConfig<DateTime>.DeSerializeFn = date => DateTime.ParseExact(date, "dd/MM/yy", CultureInfo.InvariantCulture);
Hope that helps.

Related

Kendo CurrencyTextBox displays 200.00 as 20000

I am using kendo UI to create an input box,
I want to automatically display the amount user have to pay in the textbox before,
so that user will only have to insert an amount if it differs from default
The data is given as decimal number.
I have tried:
#(Html.Kendo().CurrencyTextBoxFor(model => model.Paid).Format("R#0.00").Min(0))
And now also:
#(Html.Kendo().NumericTextBoxFor<decimal>(model => model.Paid).Format("R#.##").Min(0))
for some reason the textbox just keeps displaying two extra zeros
For some reason the textbox just keeps displaying two extra zeros
Yes, as per my investigation upon your code snippet it might be because of your default-culture. However, we can customize it as per the requirement. You could follow below steps:
Program.cs:
var defaultCulture = "en-US";
var ci = new CultureInfo(defaultCulture);
ci.NumberFormat.NumberDecimalSeparator = "."; // Defining my preferrence for number
ci.NumberFormat.CurrencyDecimalSeparator = ".";
// Configuring Number Seperator Using Localization middleware
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture(ci),
SupportedCultures = new List<CultureInfo>
{
ci,
},
SupportedUICultures = new List<CultureInfo>
{
ci,
}
});
Note: As you can see in NumberFormat.NumberDecimalSeparator I am setting my preferrence for seperator. You are open to use anything you want, any kind of charaters. Please consider the order as well while placing the code in middleware. Best use would be end of your current middleware. As following:
Update for Classic Asp.net 4.8 and Older:
We can configure above steps for asp.net classic project as well. To set the UI culture and culture for all pages, add a globalization section to the Web.config file, and then set the uiculture and culture attributes, as shown in the following example:
<system.web>
<globalization
culture="en-US"/>
</system.web>
Note: More details can be found here in official document.
Output:
Note: For further reference you could check our official document below.
Custom numeric format strings
The "." custom specifier

Sending out incorrect Date after app restart

All DateTimes are stored like this in the database:
myDate.ToUniversalTime()
I can see the dates are stored correctly in the database i.e. in UTC format.
When I, from an Android application created in Xamarin, create an object using parameters the newly created object is returned to the application and the UTC time is sent back correctly in that object.
However, when I close the app the same object does now have another timestamp i.e. not the UTC time stamp.
App -> CreatePayment(int amount) -> ASP.NET MVC does it's magic and returns the newly created object for the app to save locally.
In an example object the returned date was 21/10/2016 21:53:01.
Now I restart the app and the returned date value is 22/10/2016 4:53:01.
How could this happen?
Do I need to set some setting so that ASP.NET MVC knows that the dates loaded from the database are in UTC format?
Can Application_BeginRequest cause these problems with my settings?
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies["Language"];
if (cookie != null && cookie.Value != null)
{
var abbrivation = cookie.Value;
Thread.CurrentThread.CurrentCulture = new CultureInfo(abbrivation);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(abbrivation);
}
else
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("es");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("es");
}
}
You can try to add <globalization> parameters in the web.config as shown below:
<system.web>
<globalization culture="es-MX" uiCulture="es" />
</system.web>
On the other hand, if the problem cannot be solved by applying this settings, you might have a look at MVC - UTC date to LocalTime.

.NET MVC UK date format won't validate despite culture en-GB

I have a series of web forms in MVC. After some updates to the website recently the DateTime field validation is assuming dates are in US format and therefore days greater than 12 are failing validation, despite the fact that I've specifed the edit date format, the validation code and the culture as en-GB. Tried various browsers, all with language set as English (United Kingdom) and validation fails in Firefox, Chrome and Edge, and only works in IE. Tried various other suggestions found here including ModelBinding but nothing seems to work.
In the class:
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
In web.config:
<globalization culture="en-GB" uiCulture="en-GB" requestEncoding="utf-8" responseEncoding="utf-8" enableClientBasedCulture="false" />
In global.asax:
CultureInfo newCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
newCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
newCulture.DateTimeFormat.DateSeparator = "/";
Thread.CurrentThread.CurrentCulture = newCulture;
Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en-GB");
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
In jquery.validate.js:
date: function (value, element) {
$.culture = Globalize.culture("en-GB");
var date = Globalize.parseDate(value, "dd/MM/yyyy", "en-GB");
return this.optional(element) ||
!/Invalid|NaN/.test(new Date(date).toString());
}
Any other suggestions?
I got this to work correctly with MVC 5. There were two changes needed:
To add a model binder, as the default binder does not take into account the CurrentUICulture. I did this with client validation removed just to test it. The reason why it doesn't by default is you might use dates in URL's, although I think if I did I would want to format the URL along yyyy-mm-dd lines.
To alter the client validation to take into account UK dates. I am not sure your jquery.validate.js code would work. I also had a bit of trouble with this as well, so in the end I installed nuget package jQuery.Validation.AdditionalMethods and made code that used that.
My own blog post at http://www.ablogaboutcoding.com/2017/08/12/mvc-uk-date-issues/ explains further.

Default decimal separator on IIS

I'm facing a rather weird issue. I have an ASP.Net website that basically allows users to deposit money into a fake account. I have some validation on the input amounts and it has a min value of 0.00. However, when I load the page and check "min value" it is 0,00 - so when the check happens on the backend the min value is 0,00 (note the separator) and it breaks/is invalid.
I set VS to use the correct culture in my config, I have set my decimal to default to use a full stop separator system side (in region/locale settings) and my IIS .Net Globalization is set to use the correct culture. However, whenever I load a page it defaults to 0,00 instead of 0.00. Is there anywhere else I need to change it?
When my friend hosts the same site on his PC the min value is 0.00 - we've checked and all our number formats and culture stuff seems the same. Is there anything I might be missing?
Your Windows/.NET Framework is probably a non-English installation? Did you use this line in your web.config
Example for US English:
<configuration>
<system.web>
<globalization uiCulture="en" culture="en-US" />
...
See: How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization

how to change en-US dates to en-GB for asp.net?

on a developer machine (cassini)
new DateTime(2012,3,14).ToString("d")
results in
14/03/2012
which is correct but when deployed to a full IIS server the result is
03/14/2012
The server is set in control panel/Region language to all English/UK/GB, running date in command prompt returns the dd/MM/YYYY format.
The site is set for both uiCulture="en-GB" and culture="en-GB" and these show in the web.config globalization tag.
I can work around this issue by adding a forced culture
new DateTime(2012,3,14).ToString("d", new CultureInfo("en-GB"));
but I would really like to know what is setting the format incorrectly.
CultureInfo.CurrentCulture.Name, CultureInfo.CurrentUICulture.Name
both return en-US
en-US: M/d/yyyy (e.g. 3/14/2012)
en-GB: dd/MM/yyyy (e.g. 14/03/2012)
Actual value in web.config
<globalization requestEncoding="UTF-8" responseEncoding="UTF-8" uiCulture="en-GB" culture="en-GB" />
I managed to get it working by putting this into the web.config
<globalization culture="en-GB"/>
In your web.config add
<globalization culture='auto' uiCulture='auto' />
and then, assuming the browser is correctly configured to pass the preferred locale, the worker thread processing the request will have its CurrentCulture and CurrentUICulture set correctly.
Any locale dependent operations (including such things as DateTime format d) will use the client's preference.
Globalization element of web.config on MSDN: https://msdn.microsoft.com/en-us/library/ydkak5b9(v=vs.71).aspx

Categories