Wednesday, 29 April 2009

Content Disposition in different browsers

Today I had to resolve an issue where in different browsers the filed dynamically generated download worked very differently / at all




The setup, we had an xml file with a custom extension, say .mj, which was being served up by ASP. The HTTP Header had a content disposition header and a response type set.



Response.AddHeader "Content-Disposition", "attachment; filename=""our file.mj"""
Response.ContentType = "text/xml"

This worked fine in Internet Explorer, the file was downloaded as "our file.mj". However FireFox and Chrome acted very differently, in FireFox the file was downloaded as just "our", and Chrome as "our file.xml".


In FireFox it appears that the issue is caused by having a space in the file name, this forum post by funkdaddu helped me on this, so by removing the space FireFox could now download the file as "ourfile.mj".




Chrome however did not want to play ball. It was still insisting on changing the file extension to ".xml". I guessed it was because we were telling it we serving up text/xml mime/type under a different file extension, I decided to change the response type to "Application/Save" just to see if this would make a difference, and amazingly it did. Amazing!




So there we have it, changing the file name to have no spaces and ensuring that the content type is set to "Application/Save" seems to make all browsers behave at least some what consistently with the Content Disposition header. Its worth noting that Scott Hanselman has a great blog post The Content Disposition Saga which talks alot about how the different of IE handles it. Also GreenBytes has a ton Test Cases for HTTP Content-Disposition header which I certainly found helpful.

Thursday, 23 April 2009

Looking for a URL using Linq and SiteMaps

I've been off sick from work with Man Flu but this afternoon I was getting bored of staying in bed for the second day so I got out my laptop just to have a play in between blowing my nose.




I wanted to write a quick way of looking up the full url path for a page within a sitemap. The only bit of information I know is the key of the page. Now I knew that I could possibly make use of the IndexOf method. This expects a SiteMapNode of the value you are looking for and returns the index of the node, you then need to get the node out of your collection of nodes, example below.




SiteMapNodeCollection nodes = SiteMap.RootNode.GetAllNodes();
int nodeIndex = nodes.IndexOf(new SiteMapNode(SiteMap.Provider, pagekey));
return nodes[nodeIndex].Url ?? String.Empty;


Now that method does work, however it felt dead clunky, I was sure I could write a more .Net 3.5 shiny one line way of doing this using Linq. In fact it was dead easy. First I still needed to get all the nodes, SiteMap.RootNode.GetAllNodes(), but I then cast these to SiteMapNode, I could then use FirstOrDefault with a nice lambda expression that matches the key property. Code below.




SiteMapNode node = SiteMap.RootNode.GetAllNodes().Cast().FirstOrDefault(n => n.Key.Equals(pagekey));
return node != null ? node.Url : String.Empty;
//note: the </sitemapnode> is not part of the code, the code highlighter JS is randomly adding it.


Simple, my only concern is that the bigger the sitemap becomes the more memory / slower this becomes, especially if called multiple times per page. It may make sense to cache the sitemapnodecollection for the duration of the page however that's beyond the scope of this brief article.




What hopefully you can see though is that a little bit of Linq and Lambda expressions can take chunks of code that seem long winded and turn them into nice neat one liners, which I think is usually more readable.

Wednesday, 22 April 2009

Book Review: ASP.Net MVC 1.0 Quickly

So this month I again have the privaledge of writing another book review. This time in an area I have particular interest. ASP.Net MVC has recently been released and there are no end of books coming out of the market, one of these being Maarten Balliauw's ASP.NET MVC 1.0 Quickly.



When the book arrived I first noted how it used the traditional orange Packt colour scheme with an interesting picture of a pair of glasses on the beach. This I preferred over the look of the last book I reviewed, however I am yet to work out the significance of the picture if there even is one?



The book starts by saying that the book will take you through "the essential tasks" and "does not cover every single feature in detail". This is my opinion is not a bad thing. It is not a full reference book like ... but more of a rapid guide to get developers to start using MVC and know the basics preety much everything, you can then get the in depth knowledge as you go along.



The book covers the following topics, not necessarily in order:


  • What MVC Is

  • Brief comparison between ASP.NET web forms and ASP.NET MVC

  • What a Controller, View and Model is and how you go about creating them in VS

  • What the process of a page is and how you handle interactions

  • What is routing and what you can do with it

  • Customizing the framework

  • Using Web Forms features in MVC

  • JQuery and AJAX in MVC

  • Testing and Mocking

  • Deployment




It then has three appendices which I recommend NOT skipping, It has a full application with source code, information on the MockHandlers available and finally tons of links on where to get more information on topics. The links help to fill in the gaps that the book has left due to its "quickly" approach and for me at least have been the most thumbed pages of the book.




The format of the book is very clear, lots of examples in C#, screenshots where appropiate and well worded. In particular I like areas where Maarten Balliauw takes the time to explain all the options for an attribute. For example in Chapter 4 he outlines Action Method Attributes, rather than just give a brief description of what they are, Marten takes the time to outline briefly all the possible attributes with a simple piece of source code if appropiate. Again this highlights how the book is just trying to make you aware of what exists so when you come to write something you think about what you could use and then go away and find out more if needed.



I have to admit I really like the book as an intro into MVC and to get people aware of it, it highlights alot and get's you thinking about design methodologies. It's one I recommend others to read.


Scores


Presentation 8/10 - Overall this book feels well put together and everything is clearly laid out


Code Examples 9/10 - Quite a high score for this, the book it littered with short code snippets and examples but what really does it for me is the example application included in the appendices. Simply working through this highlights everything you have read so far and highlights more. Well worth looking at


Quality of Content 8/10 - Again repeating what I have said earlier but I feel the content has been well put together and arranged in a manner that is clear and conjusive to learning


Overall 8/10 - If you are looking at just finding out about this ASP.NET MVC is all about and just want an outline to get you started this book is for you. It's not claiming to be a reference but a starting block to use to get you started, the links in the back give you some where else to go afterwards. Well worth a read.