Web Development

Tips, tricks, experiences, and fun with ASP.NET, jQuery, and other web-related tech.

FluentValidation and MVC - From Server to Client

We do a lot of interesting stuff with FluentValidation at work and more than a few times I've had to give the whiteboard presentation of how a server-side FluentValidation validator makes it to jQuery validation rules on the client and back. I figured it was probably time to just write it up so I can refer folks as needed. Let's start out with a simple model we want to validate. We'll carry these examples with us in the walkthrough so you have something concrete to tie back to. public class MyModel { public string Name { get; set;...

Adding Custom Files to an MSDeploy Package

As part of some of my web projects I have "plugin assemblies" that aren't directly referenced by the project but are things I want included in my deployment package. I tried following the instructions on this fairly popular blog entry, but it didn't seem to work - that blog entry tells you to modify a set of files during a stage in the packaging pipeline "CopyAllFilesToSingleFolderForPackageDependsOn" and that target never actually fired for me. In fact, I threw an <Error> call in there just to see if I could get the build to fail and… no luck. It also...

How to Set Up JSHint in Sublime Text 2 Using SublimeLinter

I’ve got Sublime Text 2 and I love it. (I even have a package I wrote myself for it.) I wanted to get JSHint going in it and I saw that there was a nice JSHint package… but then I found SublimeLinter, which seems to be the King of Sublime Text Linting Packages. It basically can run any lint for any program type and gives a nice inline highlighting for errors. Very cool, very flexible. And very hard to figure out all the steps needed to get JSHint up and running properly on a clean system. So here’s...

Unfortunate Things with JSON, jQuery UI Datepicker, and Dates

I’m trying to write an HtmlHelper extension that lets you dump out options that build a jQuery UI datepicker on the client. (I know there are some of these out there, but for various reasons I won’t get into… I get to make one, too.) Something that will let me do this: @Html.DatePickerFor(m => m.SomeDate); In doing that, I’ve uncovered a lot of frustration around getting the options specified on the server over to the client and working in a consistent format. JSON date serialization continues to be a joke....

Putting Controllers in Plugin Assemblies for ASP.NET MVC

With Autofac (particularly the multitenant extensions) I see a lot of questions come through that boil down to this: I have an ASP.NET MVC project. I have some controllers in a plugin assembly that isn't referenced by the main project. At application startup, I do some scanning and use Autofac to dynamically register the controllers. For some reason I get an error when I try to visit one of these controllers. How can I have a controller in a plugin assembly? Shannon Deminick has a nice blog article that explains this in a different context - similar question,...

jQuery.each and Object Boxing

I just spent like six hours trying to figure this out, so profit from my experience. I have some custom code wiring up jQuery Validation in a form and for some reason, the jQuery "html" method was causing a "HIERARCHY_REQUEST_ERR: DOM Exception 3" whenever validation errors were being displayed. After banging my head against it for a while and getting some help from a co-worker, we found it came down to the $.each iterator. I was compiling up a list of error messages and using the $.each to add them to settings for a jQuery validator. It looked...

Windows "Command Prompt Here" Generator

I love context menu "command prompt here" shortcuts. Every time a new VS version comes out, or a different prompt is available that I need, I create a new one. In the past, I've tried to maintain a whole roundup of current versions, but that's been tough. Different operating systems come out, I need to tweak this or that to make things work, and I end up having to edit and re-package the whole bundle. It's also a pain for people wanting the installers – you want some, but not all; the tool isn't quite what you want but...

Html.DemoPartial - Side-by-Side Render and Code for Partial Views

I was working on an ASP.NET MVC site for demonstrating some internal concepts recently. One of the things I wanted to show was how to use some HtmlHelper extension methods. I wanted the viewer to be able to see the rendered output and then flip to see the code, sort of like how you see in snazzy demos like the DevExpress MVC Extensions. Here's a little screencast/demo of what I'm talking about. Unable to display content. Adobe Flash is required. So... how do you do that? Here's how it...

ASP.NET MVC, aria-required, and jQuery Unobtrusive Validation

I saw this interesting article about using model metadata in ASP.NET MVC to add the aria-required attribute to required input fields. The approaches there are all totally valid and work great if you have the requirement to add the attribute on the server-side. However, if you're using jQuery unobtrusive validation, you've already got some metadata you can use from the client side. Required fields all get the data-val-required attribute associated with them: <input data-val="true" data-val-required="This is a required field." id="RequiredField" name="RequiredField" type="text" value="" /> The attribute only exists on required fields. With that,...

Data Type Validation and Model Binding in ASP.NET MVC

When validating input in a web forms application, you need to validate data types on the client and server side because you're working with text boxes and server controls. When you move to MVC, the client-side validation is still an interesting problem to solve, but the server-side validation all happens as a by-product of model binding. The DefaultModelBinder has some special built-in provisions to handle data type parsing errors and automatically convert those into standardized model state errors. If you're writing a custom model binder and you want to participate in this... Get the...

Firefox User Agent "undefined GoogleToolbarBB"

At some random point today, I started going to various web sites and they seemed to be falling back to basic views without anything dynamic going on. For example, in GMail, I saw "Some important features may not work in this version of your browser, so you have been redirected to the Basic HTML version. Upgrade to a modern browser, such as Google Chrome." Weird, because it was just working a second ago. Realizing it was not properly detecting my browser, at first I thought GMail just didn't support Firefox 8, then I thought, "No, that's silly, something...

MVC3 Project Template Requires Solution Node in Solution Explorer

I've got Visual Studio 2010, MVC3, and the latest tools update. I'm all patched up. But I'm seeing something weird. I start out and I have a solution with a single C# class library in it. I decide to add an MVC3 site to it, so I do File -> Add New Project. I select an MVC3 project. I click OK, and I select an Empty web application. When I click OK on this screen, I get an error "Cannot add the item because the item to add it to...

Mozilla Firefox with Hanselman

Bing released a Firefox With Bing addon, which prompted Scott Hanselman to tweet a bit: Looking at the addon code, it was released under MPL 1.1/GPL 2.0/LGPL 2.1 so... In response to the peoples' demand, I present to you Firefox With Hanselman (Click to get the addon.) This addon does pretty much exactly what the Bing addon does: Set your home page to http://www.hanselman.com/ Add a search provider so you can search Scott's web site. Free. No warranty expressed or implied. Works...

Manually Resetting the Automated Firefox Software Updater

I run in Windows as a non-admin user. Whenever I need to install something and Windows prompts for credentials, I have a whole separate user account that runs admin tasks. This appears to be a problem for the automated Firefox software update process. What I run into goes something like this: Open Firefox and get notified there's a software update. Click the button to close Firefox and apply the update. Firefox closes and prompts me for admin credentials, which I provide. The update...

Long URL Path Segments in ASP.NET Routing Yield 400 Errors

Ran into an interesting gotcha while working with routing and a handler on a web site. We had a route set up like this: CustomHandler/{value} Nothing too special, except the "value" route parameters are fairly long strings. In some cases 300 characters or more. We were finding that in some cases things worked great, but in others IIS would return a 400 error claiming "invalid URL." The URL total length was less than 1000 characters, so it wasn't that. Turns out there's a registry setting for indicating the maximum length of any individual path segment...