Code Snippets

Handy blocks of code you can use.

How to Add a VSIX Installer to a DXCore Plugin

As of CodeRush/Refactor/DXCore 11.2, DXCore supports installation of plugins using Visual Studio Extensibility (VSIX) just like any other extension you might find in the gallery. This is beyond cool for a couple of reasons. First, I've been [slowly] working on a couple of different ways to create some nature of "plugin gallery" for DXCore extensions. There are so many community plugins, plus things like CR_Documentor and CR_CodeTweet that aren't on the community plugin site, that it's really hard to know what's out there. This would allow DXCore plugins to use the standard Visual Studio Gallery mechanism for browsing. ...

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

Explore From Here - Command Line

I'm a big fan of the "command prompt here" context menu extensions for Windows Explorer. I use them all the time. Sometimes, though, I need to go the other-way-around. That is, I'm at a command prompt and I want Windows Explorer open at the current location of my prompt. explorer %CD% Pretty simple, but super helpful. I had one of those "man, I'm stupid" moments when I put two-and-two together on this. I ended up making a little batch file "explore.bat" and stuck it in my path. @echo off echo Opening Explorer on %CD% explorer %CD% So now...

Choosing an Exception Type When Unit Testing Error Handling

When I'm testing exception handling code, I have tests for exceptions I know I need to handle and tests for exceptions I'm not expecting. For example, say I have a component that calls a WCF service. If there's a communication issue, I want to mask that and return some stub/placeholder data. If there’s some other issue, I want to just let the exception bubble up and be handled by a global error handler. Something like this: public DataObject GetData(SomeParameter p) { if(p == null) { throw new ArgumentNullException("p"); } DataObject data =...

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

Add Downloaded iTunes Art Directly to Track

In iTunes, if you have a track that is missing artwork you have the ability to right-click the track and opt to automatically download artwork for it. This works well if you play the track in iTunes or an iTunes-connected device (e.g., iPod)... but if you also use the same library in a UPnP server to stream your music on your network (like Asset UPnP) then you'll notice the artwork doesn't show up. That's because iTunes stores the downloaded artwork in a separate database outside the actual physical music track file, but other servers/devices expect artwork to be embedded in...

Working with Windows Identity Foundation in ASP.NET MVC

If you've worked with Windows Identity Foundation, you'll find it very nearly mandates that you implement a passive security token service using classic ASP.NET web forms rather than MVC. It doesn't lend itself well to testability, and in some cases it writes content directly to the response stream without you being able to govern when/how that happens. All is not lost, though. Here are a couple of helpers and tips when working with Windows Identity Foundation in ASP.NET MVC to create a passive STS. First, drop what you're doing and go buy a copy of Programming Windows Identity...

Fixing the Desktop Icon Drop Shadow Problem on Windows Server 2008

I develop on a daily basis on a Windows Server 2008 R2 machine. I do that because that's my target deployment environment and it's really helpful to be able to actually run the full product and debug right there on my "workstation." As such, I have the full "desktop experience" enabled - Aero, themes, the whole bit. One problem I noticed was that the drop shadows under the icons on the desktop... they just don't stick around. I set my visual effects settings to "best appearance" and everything looks correct, but if I log out and back in, the...

Posting multipart/form-data Using .NET WebRequest

While making my ImageShack plugin for Windows Live Writer I had to figure out how to make a web request that posts data to an endpoint in "multipart/form-data" format. That's a lot different than URL-encoding everything into a querystring format and sticking that in the body of the POST request. If you're uploading one or more files, you need to format things differently. I looked all over for some built-in function that does this, but it doesn't seem to exist in the .NET base class library. Surprising, but also not surprising. I found a question on StackOverflow that talked...

Putting log4net.config Outside of Application Configuration

I use log4net for logging in various applications, but every time I start a new app I forget this and it never quite comes up in Google for me, so here we go. Most of the examples on the log4net site showing configuration shows it right in the App.config/Web.config file for the application. That's a painful way to go if you have, say, a single log4net.config that you want used in several projects or if you otherwise want to stick log4net.config somewhere else. The magic bit that at least I can't easily find and always forget is: ...

Context Menu Icons with DXCore

A long time ago I posted a little sample showing how to get context menus working with your DXCore plugin. As part of a new plugin I'm working on, I wanted to get an icon to show up in the context menu next to my entry in the menu, something like the icon you see for the Refactor! menu: It's possible, but it's not really documented, so here's what you do. First, make sure you're working with a DevExpress.CodeRush.Menus.IMenuButton. This is the type of thing you can click on and have something happen, as opposed to a DevExpress.CodeRush.Menus.IMenuPopup which is what...

Convert a Relative Path to Absolute Path with jQuery and ASP.NET AJAX

I was messing around with relative paths to files (e.g., "../images/error.gif") and needed to convert them to absolute paths (e.g., "http://server/images/error.gif") on the client but couldn't figure out how. Then I saw this nifty trick to HTML encode things using jQuery and it gave me an idea. String.toAbsolutePath = function(relativePath) { /// <summary> /// Converts a relative file path into an absolute file path. /// </summary> /// <param name="relativePath" type="String"> /// The string with the relative path, like "../foo/bar.gif" /// </param> /// <returns type="String" /> var path = $("<div style=\"background-image:url('" +...

Parsing Currency Values with ASP.NET AJAX

I had to parse a culture-sensitive currency value from a string and couldn't figure out how to do it. I'm using ASP.NET AJAX to do String.localeFormat("{0:c}", value) for writing a currency value to a textbox, but getting it back out... not so easy. The Number.parseLocale extension provided with ASP.NET AJAX is cool for parsing out numbers in a culture-sensitive fashion... if they don't have a currency symbol. So, time to hook that up. Here's what I came out with: Number.parseCurrency = function Number$parseCurrency(str) { /// <summary> /// Parses a string containing a culture-sensitive currency value into a number. ///...

Replace jQuery Validation Message Formatting with ASP.NET AJAX String.format()

I'm working on a site where we're using both ASP.NET AJAX and jQuery to get things done. This includes jQuery Validation for client-side validation functionality. One of the things that comes with jQuery Validation is a $.validator.format method that replaces the {n} style parameters in a string with arguments - basically, a very lightweight String.format. ASP.NET AJAX provides a really nice implementation of String.format that is pretty full-featured and understands format strings. For example, you can do String.format("{0:d}", mydate) to format a date in short date format. Snazzy stuff. Unfortunately, the jQuery Validation one isn't that robust... so I figured I'd marry...

Getting the Windows OS Version in MSBuild

I saw a tweet come across asking how to get the OS version in MSBuild. MSBuild will automatically import any environment variables... but it appears the OS version isn't an environment variable, so it doesn't have any OS version info you can get out of the box. You can, however, do a registry key lookup. Here's a quick MSBuild snippet showing how to get the Windows version out of the registry. If you want more version-related information, there's a lot in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion. <?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="DisplayVersion" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <OsVersion>$(registry:HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion@CurrentVersion).$(registry:HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion@CurrentBuildNumber)</OsVersion> </PropertyGroup> ...