ASP.NET MVC Localize Numeric (data-val-number) Validation

Another buggy experience in ASP.NET MVC. This has to do with validation, more specifically numeric validation. Whenever you have a numeric property on your view models, ASP.NET MVC automatically adds some implicit validation to ensure that whatever entered in fact is a numeric value. Might be good in most cases, but here’s the problem. The validation mechanism writes the error messages in english. So for any app that targets a non-english audience, this needs to be changes. But that’s easier said than done. The code in ASP.NET MVC that generates the error message for the implicit numeric validator, looks like this: private static string MakeErrorString(string displayName) { // use CurrentCulture since this message is intended for the site visitor return String.Format(CultureInfo.CurrentCulture, MvcResources.ClientDataTypeModelValidatorProvider_FieldMustBeNumeric, displayName); } To make matter worse, this is a method from an internal, sealed class. They didn’t want you to change this. … Continued

10 Ways to (re) Structure JavaScript

Structuring JavaScript can be a job in itself if you don’t look out. I was attending the virtual jQuery Summit 2011, and one of the talks were called ‘Structuring Your DOM-based Application‘ by Garann Means. Her talk really took me back to the days of fighting un-structured JavaScript. I was working on a code base that had been maintained by a handful of different developers, each with different skills, ideas and disciplin. Web development, and especially JavaScript requires a lot of disciplin in order to keep the code clean and structured, and that just wasn’t the case at that time. While I was watching Garann’s talk, I constantly thought of all the problems we had and what we eventually did to solve our issues and take control of our JavaScript. This is is my 10 ideas on ways to (re) structure JavaScript, … Continued

JavaScript Bundling Issues When Using Unfinished Closures

We all know it’s a good idea to bundle JavaScript files into as few script includes as possible to minimize HTTP request. And – we all know you should write your code inside closures, to avoid the biggest problem of JavaScript: its dependence on global variables! But what happens when those two things don’t work together? For me, they didn’t play nice with each other today. Whenever I bundled my JavaScript files i got an Uncaught TypeError: undefined is not a function. Consider this code: /****** a-file.js ******/ !function($) { // Code here }(window.jQuery) /****** another-file.js ******/ (function ($) { $(“#hello”).text(“World!”); })(window.jQuery);​ The error occurred on the first line, inside the closure in another-file.js. Take a look at this jsFiddle. Since the result of the first closure is true, the above code will evaluate as this: /****** a-file.js ******/ true /****** another-file.js ******/ (function … Continued

ASP.NET MVC 3 Windows Authentication problem – redirects to Account/Login

ASP.NET MVC 3 has its quirkiness here and there. Sometimes it’s a lack of support for something, and other times it’s downright buggy. An issue I came across recently, is something to do with Windows Authentication. There’s a lot of resource on the web on how to run ASP.NET MVC with Windows Authentication. There’s even a project template, the Intranet site, which ships with MVC 3. It should be a no-brainer! The issue I came across, prevented ASP.NET from using Windows Authentication. Even though I had set authentication mode to Windows in Web.config: <authentication mode=”Windows” /> <authorization> <deny users=”?” /> </authorization> And disabled anonymous authentication on the website in IIS - it always redirected all requests to /Account/Login, as if it was using some sort of default Forms Authentication. This coursed a server error – the dreaded Yellow Screen of Death, since I … Continued

Make CSS inline in C# and ASP.NET using PreMailer.Net

Being able to make CSS inline is something you need all the time when sending HTML e-mails. HTML E-mails simply don’t render perfectly in all e-mail clients if you don’t make all your styles inline. Campaign Monitor introduced the ability to make CSS inline years ago, and it just works. They use the premailer Ruby project to do this, and it seems to work perfectly. You can edit your HTML e-mails using styles embedded inside the HTML document’s <head> tag, and they automatically make the CSS inline on all elements that match your selectors. At the end of my latest blog post on How to Send HTML E-mail from an ASP.NET MVC Controller, I wrote that I’d like to combine the ActionMailer.Net library with the premailer Ruby project, to be able to send great HTML e-mails from an application, that renders well in … Continued

How to Send HTML E-mail from an ASP.NET MVC Controller

As I argued about a year ago, any modern piece of software needs to send e-mail. It needs to connect, and reach its users. Back then I showed how to send templated HTML e-mail, using the little known MailDefinition class in C#. Today, there’s an even better approach. The main problem with the old solution, was that the templates were plain, old, flat and boring HTML templates. Any sort of dynamic content, were limited to what a simple replace could do. The better approach that I’m using now, is the awesome ActionMailer.NET package. It’s right there within Visual Studio, if you have Nuget – which I’m sure you do. Why is it better? ActionMailer.NET is better, first of all because the templates for your e-mails are just regular ASP.NET MVC views. That means you can strongly type those view and … Continued

Serialize C# dynamic and Anonymous types to XML

Dynamic / Anonymous types in C# was a great improvement to the framework, made in version 3.0. I use it a lot when doing AJAX callbacks from JavaScript to ASP.NET MVC Controllers, not to forget the extensive use of anonymous types already used in ASP.NET MVC. Then yesterday, one case where I absolutely needed to use anonymous types was in my application’s logging service. I want to be able to save behaviors/actions as well as errors. I have two separate tables, but both behaviors and errors can provide a set of details. To do the actual logging, I call an implementation of this interface: /// <summary> /// Defines methods for logging errors and behavior. /// </summary> [ServiceContract] public interface ILogService { /// <summary> /// Logs the behavior with data. /// </summary> /// <param name=”applicationInstanceId”>The application instance id.</param> /// <param name=”action”>The … Continued

Making your ASP.NET Global Resource files work in JavaScript. IntelliSense included!

Any modern web application needs localization! You simply can’t ignore the huge amounts of people who doesn’t speak your language, or whose native language is different from yours. You’re probably using resource files (.resx) in .NET, but how do you go about getting values from your resource files in JavaScript? Rick Strahl wrote a great blog post about an HttpHandler that serves the content of your resource files in JavaScript. You basically add a script tag to your page that points to the HttpHandler, and the HttpHandler will produce the resources in JavaScript as an object with properties on it, like this: [code lang="js"]var localRes = { AreYouSureYouWantToRemoveValue: "Sind Sie sicher dass Sie diesen Wert l\u00F6schen wollen?", BackupComplete: "Der Backup f\u00FChrte erfolgreich durch", BackupFailed: "Der Backup konnte nicht durchgef\u00FChrt werden", BackupNotification: "Diese Operation macht einen Backup von der Lokalisationtabelle. \nM\u00F6chten … Continued

Translate text in C#, using Google Translate, revisited

A long time ago, I wrote a blog post about how to translate text in C# using Google Translate. Since then, an official AJAX API has been released, which is a much better solution. Reading the comments of the blog post indicates that the code was broken quite quickly! Google’s AJAX Language API, also includes functionality to detect language from a given string. All you can do on translate.google.com you can do via the API. Reading the class reference for the Translation API, for Flash and other Non-Javascript environments gives us the information we need to perform translations from C#. Most important is the URL and the parameters required and accepted. Another very important thing we can read in the documentation is this: Applications MUST always include a valid and accurate http referer header in their requests We must remember … Continued

The (almost) Ultimate Developer PC 2.0

There’s a lot of talk on Twitter and blogs about The Ultimate Developer PC 2.0. The Ultimate Developer PC, was something started three years ago by Scott Hanselman and Jeff Atwood, when Scott wanted a new machine and went for the best of the best. Jeff Atwood came up with his take, known as The Coding Horror Ultimate Developer Rig. The same story goes for The Ultimate Developer PC 2.0, only that now it’s Scott Hanselman and Pete Brown building their new machines with an obession of having to achieve a 7.9 score on the Windows Experience Index (WEI). Back in August 2007 with The Ultimate Developer PC 1.0, Scott Hanselman achieved a WEI of 5.8. I guess what started the 7.9 WEI obsession, is the WEI Share website where you can show off your score, and throw down your … Continued