Archives - Category: Dev
Handling case-insensitive enum action method parameters in ASP.NET MVC
Using enums as action method parameters
Say you have a enum, a sorting enum in this case.
public enum SortType { NewestFirst, OldestFirst, HighestRated, MostReviews }
ASP.NET will gladly let you use that enum as an action method parameter; you can even make it an optional parameter. To make it optional by routing, you need to make it nullable for the action method function parameter in your controller and add some guarding logic (!sort.HasValue or the like).
Subtleties with using Url.RouteUrl to get fully-qualified URLs
At some point I missed the Url.RouteUrl overload that took a protocol and returned an absolute URL based on the current context. It is quite handy when you are sending URLs out into the world (e.g., RSS feed link). I ended up using the less-handy overload that took an explicit host (same as the current, in this case) and passing it in. When someone pointed out the simpler overload, I did the obvious and deleted the host from the call. That didn’t quite work.
For those looking for a way to get a fully-qualified URL through the route system, the less-than-obvious answer is to call the overload for Url.RouteUrl that gets a URL with a different protocol (
Url.RouteUrl(string, object, string)
), passing inRequest.Url.Scheme
for the current protocol.Getting dynamic ExpandoObject to serialize to JSON as expected
Serializing ExpandoObjects
I am currently creating a JSON API for a handful of upcoming Sierra Trading Post projects. When I found myself generating JSON for a stripped-down representation of a number of domain classes, all wrapped with some metadata, I turned to
dynamic
and things have been going quite well. Unfortunately, there was a hurdle to getting the JSON to look the way I wanted.If you start playing around with serializing
ExpandoObject
to JSON, you will probably start finding a number of options. The easiest solution to find is the one that comes with .NET,JavaScriptSerializer
underSystem.Web.Script.Serialization
. It will happily serialize anExpandoObject
for you, but it probably won’t look the way you expect. Your searches will probably vary, but I found Newtonsoft’s JSON.NET, which handledExpandoObject
right out of the NuGet box. Then I stumbled on ServiceStack.Text (also “NuGettable”). While it does even weirder things than the .NET serializer withExpandoObject
s, it supposedly does them very fast.Adapting Visual Studio code styling differences for open source project contribution
Background
Today, while incorporating Lee Dumond’s MVC Route/URL Generation Unit Tester into a project, I found a desire to contribute some code I thought would make the package easier to use. Unfortunately, the project code formatting looks nothing like my preferred conventions (some form of 1TBS, I guess). Until Visual Studio offers a way to distribute code style settings to source control consumers easily, I needed a different option.
While preparing demos for a mobile web development talk for the Cheyenne Computer Professionals group, I stumbled on Mike Minutillo’s tip for running a “demo” instance of Visual Studio where I could sandbox my presentation settings optimized for an elderly VGA projector. This sparked an workaround idea for dealing with multiple formatting settings of various projects I may work on.
Rather than force my conventions on the project (generally not acceptable) or give up on my own style (generally not acceptable), I decided to try using a “demo” instance of Visual Studio with that projects styling conventions set.
From jsFiddle to GitHub to AppHarbor to stackgeography.com
Update
I finally put together an app listing on StackApps. If you like stackgeography.com and you have a StackApps account, I’d love for your vote on the StackGeography app page.
Background
My latest project at my full-time job with Sierra Trading Post is their Mashery-managed API. It currently includes most everything you would expect from a retail API: departments, brands, products, search, and reviews. I would like it to include a few things that are less typical but fun to work with from a data aggregation standpoint. One example: recent order locations. While it may not see a lot of outside use, it would be quite nice for our internal observation; it would go great on the handful of large screens around the company with site statistics continually updating on them.
Initial setup for a new ASP.NET MVC site in IIS7
Background
Over the years, I have spent far too many hours running the same set of commands against ASP.NET and ASP.NET MVC sites to bring them up to what I consider a starting point. Most of the time, I have to refresh myself about how to do at least one of them. This is a list of those commands in a central location for myself and anyone else to use. As with any good instructions, there is no guarantee you won’t completely destroy your server, site, or soul using these commands. I can only say they worked for me once.
Closing and Re-opening tabs in Visual Studio with Ctrl+W
Visual Studio 2017 Undo-Close Update: The Productivity Power Tools have spun off into a bunch of more-focused extensions. To get Undo Close in Visual Studio 2017, you will want the Power Commands extension now.
Visual Studio 2013 Undo-Close Update: Since the prior options for re-opening closed tabs fell apart with the release of Visual Studio 2013, you will need the newly released Productivity Power Tools 2013.
Update: now with the ability to re-open closed tabs with Ctrl+Shift+T. This also allows you to re-open tabs closed by a project file reload, which is fantastic!
Ever tried to close a tab in Visual Studio 2010/2012 with Ctrl+W. If so, you find yourself selecting the current word in your text editor (Edit.SelectCurrentWord). I don’t use that shortcut, though I could see it being handy over my usual Ctrl+Shift+Right-/Left-Arrow. I do, however, use Ctrl+W to close windows/tabs in just about every other program I use. In order to make that shortcut work for your Visual Studio editing, you just need to assign it to File.Close instead.
Performance Stub: getting all subtype items from a list
Performance Stubs
These blog posts are simply times I wanted to identify the fastest method for accomplishing a particular goal. As I write the code, I like to make some light notes of alternatives while driving forward with the first implementation that makes it from my brain to my fingers.
When I get the chance, I can go back and flesh out the two versions and drop them into some basic
Stopwatch
timing to determine which is better in terms of raw speed. Factoring those results with clarity of code, I have a method I will likely choose the next time I need the same feature.Goal
Given a particular
IEnumerable
, find all the elements that are of a particular type (subclass, in this case).Where did that JSON field go? Serializing IHtmlString to JSON
TL;DR
If your brain consumes Stack Overflow questions better than blog posts, go see “How do I serialize IHtmlString to JSON with Json.NET?” over there.
Mono for Android: "aapt.exe" exited with code 1
TL;DR
Also available in TL;SO (too long; Stack Overflow) flavor.
Getting this error:
"aapt.exe" exited with code 1
?Do you have any files in your Mono for Android solution that are being packaged together with the app (e.g., “AndroidResource” build action)?
If so, make sure they don’t have anything but letters, numbers, periods, and underscores ([a-z0-9_.]) in their names.