c# - ASP.Net Validating Culture Names -


We have a multilingual site that uses asp.net 2.0 cultures through the current culture url to a query string Has been rewritten in the form. (Spanish version of ~ / Es / blah.aspx ~ / blah.aspx is rewritten as ~ / blah.aspx? Lang = es)

The code tested for culture is as follows :

  System. Globalization. Culturfo CI; {Ci = new System.Globalization.CultureInfo (Request.QueryString ["lang"]? String.Empty); } Hold {ci = new system. Globalization.CultureInfo (string.Empty); }  

If there is no culture set, then it is a mistake for English, 127. When there is a culture, all the links on that page are written with the correct culture name in the past Are there.

Some links to some or some other form of spider ~ / www.test.com / blah.aspx, and our site is clashing with a culture of www.test.com Which is flooding and our error logging.

Is there any way to test that besides the exception, is the name of any culture valid?

I thought I would have to move a quick to measure it so that a quick console app slipped.

It basically uses all 3 methods (constructor, LINQ, and forerunner) to obtain cultureinfo from string 10000 times in the loop. I removed the stopwatch and removed the console output for a brief.

  string culture = "n-GB"; Cultureinfo [] cultures = cultureinfo Gate cultures (culture type. Specific culture); {Try {CultureInfo c = new CultureInfo (culture) for (Int i = 0; I & lt; 10000; i ++); } {}} For {{{}} (int i = 0; i & lt; 10000; i ++) {CultureInfo c = cultures.FirstOrDefault ((x) => x.Name == culture); } (Int'l = 0; i & lt; 10000; i ++) {foreign culture (cultures in cultures) • if (c.Name == culture break); }}  

The results are as follows ...

  Try catching: 00: 00: 00.0023860 LINQ: 00: 00: 00.0542459 ForEach: 00: 00: 00.0238 9 37 If you remove the cultures variable and call it every move, then take LINQ and ForEach loops approximately 2.5 seconds.  

Therefore, if you expect to receive many valid inputs and only invalid invalid one, then using constructor is favorable. But if you change the value from input N-GB to TEST , then things change on a large scale.

  Try to catch illegal culture: 00: 00: 39.7163513 Invalid culture LINQ: 00: 00: 00.0791752 for illegal culture every: 00: 00: 00.0291480  

Obviously my test application is not an actual world scenario, but since the OP said this is said on the basis of a request, I can imagine that this code can be said in a large web application . This is probably a rejection or service vector, the web server gets all the CPUs from spamming requests that all have invalid culture parameters.


Comments