Web Development

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

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...

Running Static Files Through VirtualPathProvider in IIS7

We have a custom VirtualPathProvider that serves some static files (*.js, *.css) from embedded resources in assemblies. It is similar in function to the WebResource.axd that ships with ASP.NET, but instead of having some crazy URL, you just access the file directly and the VPP finds it in embedded resources and serves it just like it was on the disk. It makes for a nice deployment experience and easy upgrade. The problem I've run into a bunch, particularly with routing showing up, is that even with a wildcard map to ASP.NET, my static files end up with a 404...

Test Your Deployed Web Site Package with IIS Express and Visual Studio

With the ability to transform your web.config file when deploying your web site came, at least for me, a question: How do I test my web site's behavior without deploying a whole copy of my web site? I figured out a reasonable, if slightly kludgy, solution and I figured I'd share. The general idea is to have a project in Visual Studio that... Acts as the point of entry for debugging the packaged version of the web site. Automatically updates IIS Express configuration to point to the packaged web site. ...

When StaticFileHandler is not StaticFileHandler

I could also have called this "wildcard .NET mapping in IIS Express from web.config." I'm sure that, like, everyone out there but me has figured this out by now, but... well, I'll blog it anyway. Problem: Your ASP.NET web site has a VirtualPathProvider that serves static files (e.g., .jpg, .css, etc.). It works great in the Visual Studio development web server but switching to IIS Express, it suddenly doesn't work. My team has just such a provider that serves static files out of embedded resources. We switched from Cassini over to IIS Express and couldn't for the...

Trying Out DevExpress MVC Extensions

I'm a huge CodeRush and Refactor user and DevExpress fan, but my day job is developing web sites so I thought I'd give the DevExpress MVC Extensions a run to see how they work. The DevExpress MVC Extensions are sort of like web controls - like HtmlHelper methods combined with some client-side script - that allow you to easily provide rich client-side widgetry in your MVC applications. I'm always interested in easy ways to provide a nice experience, so this sounds pretty good to me. The first thing I noticed, even before I got a chance to use...

Typemock Isolator 6.0.8 Fixes ASP.NET MVC3 Testing Issues

If you use Typemock Isolator and you're an ASP.NET MVC3 user, you probably noticed that some of your tests have been failing with exceptions like System.VerificationException. The latest version of Isolator, 6.0.8, fixes the issue. Head on over and update. (Or, if you haven't got a license yet, go get one!)

IIS Express Adds a (Possibly Duplicate) Certificate During Install

When you install IIS Express it also installs a self-signed certificate that it will use for SSL. This is not normally a problem, however, if you already have a self-signed certificate, it may result in a confusing issue where you have two certificates with the same distinguished name, like so: And why would that be a problem? Say you are working with WCF services that need to identify themselves with certificates. You have your dev machine set up to use the self-signed certificate, like so: <serviceCredentials> <serviceCertificate x509FindType="FindBySubjectDistinguishedName" findValue="CN=localhost" ...

Dynamic HttpModule Registration in ASP.NET 4.0

I came across this trick while perusing the Autofac code base with the new MVC3 integration. You no longer have to register the HttpModule that disposes of request lifetime scopes because they do it for you dynamically. Figuring out how they did it revealed two really cool little tricks I've not seen documentation on. Trick #1: System.Web.PreApplicationStartMethodAttribute .NET 4 adds a new attribute that allows you to programmatically do things just before application startup (that is, before Application_Start in your Global.asax). ASP.NET MVC3, for example, uses this hook to register build providers for the Razor view engine so...

Switch Default Browser in Visual Studio without "Browse With..."

Visual Studio has a nasty habit of forgetting your default browser settings at seemingly arbitrary times. Couple that with the fact that you can only really switch your default browser in VS using this "Browse With..." context menu on a browsable ASPX or HTML page (and the fact that in your MVC solutions you probably don't have a directly browsable ASPX or HTML page) and this whole thing is a nightmare. Enter Hanselman, who has posted a Powershell solution to the problem. YAY!

Book Review: Testing ASP.NET Web Applications

I got a copy of Testing ASP.NET Web Applications a while ago but it's taken me some time to get through it because I wanted to dedicate the time and attention to it that it needed. There really aren't many decent books that discuss testing all different aspects of web applications (though there are several dedicated specifically to security testing). The content in this book is something I've been trying to find for a long time in a format aggregated all together in one place and, despite a few rough edges, I'll be recommending it to my QA friends this...