- Recent
- Popular
- Tags (0)
- Subscribers (16)
- Handling Formats Based On Url ExtensionYesterday
-
Rob pinged me today asking about how to respond to requests using different formats based on the extension in the URL. More specifically, he’d like to respond with HTML if there is no file extension, but with JSON if the URL ended with .json etc...
/home/index –> HTML
/home/index.json –> JSON
The first thing I wanted to tackle was writing a custom action invoker that would decide based on what’s in the route data, how to format the response.
This would allow the developer to simply return an object (the model) from an action method and the invoker would look for the format in the route data and figure out what format to send.
So I wrote a custom action invoker:
public class FormatHandlingActionInvoker : ControllerActionInvoker { protected override ActionResult CreateActionResult( ControllerContext controllerContext, ActionDescriptor actionDescriptor, object actionReturnValue) { if (actionReturnValue == null) { return new EmptyResult(); } ActionResult actionResult = actionReturnValue as ActionResult; if (actionResult != null) { return actionResult; } string format = (contr - Fun With Named Formats, String Parsing, and Edge CasesJanuary 4
-
Recently I found myself in a situation where I wanted to format a string using a named format string, rather than a positional one. Ignore for the moment the issue on whether this is a good idea or not, just trust me that I’ll be responsible with it.
The existing String.Format method, for example, formats values according to position.
string s = string.Format("{0} first, {1} second", 3.14, DateTime.Now);But what I wanted was to be able to use the name of properties/fields, rather than position like so:
var someObj = new {pi = 3.14, date = DateTime.Now}; string s = NamedFormat("{pi} first, {date} second", someObj);Looking around the internet, I quickly found three implementations mentioned in this StackOverflow question.
- A Smarter (or Pure Evil) ToString with Extension Methods by Scott Hanselman.
- C#: String.Inject() – Format strings by key tokens from Oskar.
- Not Your Typical Top Ten Of 2008 PostDecember 30 2008
-
At the end of the year, it’s very common for bloggers to take a look back at their own blog and list their favorite 10 blog posts. I find that somewhat narcissistic, so you know I’m going to do that.
But before I do, I thought it would be great to list the top 10 blog posts by others I read in 2008. The only problem is, I have very bad short-term memory and I can’t seem to remember which ones I read that had a real impact on my thinking. I’ll have to try and keep better track in 2009.
So based on a cursory Google search and look through my Google Reader app, here are some of my favorite blog posts of 2008 by you all, I couldn’t even list 10. Please do comment with your own favorite blog posts, mine or otherwise of 2008.
Favorite Posts I Read
- IT Industry Revolutionised by Labour Saving Device This one is pure comedy gold by Leon Bambrick, aka SecretGeek.
- jQuery and Microsoft A short post, but the significance of what the post indicates is more momentous than the post itself.
- On Our Project, We’re always 90% Done What top list would be complete without a CodingHorror lin
- Interesting use of XML Literals as a View EngineDecember 29 2008
-
Dmitry, who’s the PUM for ASP.NET, recently wrote a blog post about an interesting approach he took using VB.NET XML Literals as a view engine for ASP.NET MVC.
Now before you VB haters dismiss this blog post and leave, bear with me for just a second. Dmitry and I had a conversation one day and he noted that there are a lot of similarities between our view engine hierarchy and and normal class hierarchies.
For example, a master page is not unlike a base class. Content placeholders within a master page are similar to abstract methods. Content placeholders with default content are like virtual methods. And so on…
So he thought it would be interesting to have a class be a view rather than a template file, and put together this VB demo. One thing he left out is what the view class actually looks like in Visual Studio, which I think is kinda cool (click on image for larger view).
- I Love To CodeDecember 29 2008
-
I was reading Jeff Atwood’s latest post, Programming: Love it or Leave it when I came across this part, emphasis mine.
Joel implied that good programmers love programming so much they’d do it for no pay at all. I won’t go quite that far, but I will note that the best programmers I’ve known have all had a lifelong passion for what they do. There's no way a minor economic blip would ever convince them they should do anything else. No way. No how.
Unlike Jeff, I will go that far. I love to code, and I do it for free all the time.
I don’t think Joel meant to imply that programmers would continue to write code for their current employers for free in lieu of any pay. What I think he meant was that programmers who love to code will code for fun as well as profit. They’ll write code outside of their paying jobs.
For example, I took the entire week off last week and spent lots of time with my family, which was wonderful (cue gratuitous picture of kid)! I trained my son in Ninja acrobatics.
