There was a question in a tech facebook group I co-manage about what tools you’d use if starting a new project today.
I don’t know for sure. It depends -of course- is the expected answer. For example, there’s some idea I had I mind I considered using MongoDB or CouchDB for, while still using .NET, and then I wasn’t sure if I go extra mile in DB I’d go for Node or Rails as well or would prefer .NET for my personal productivity. I also often use Node/Bower when checking libraries with many dependencies.
So, for this question, I thought what tools I might use in a company project. Thinking back, I found that most tools I use now are good enough for what they do. So, I thought I’d list these.
These tools are my personal experience though. While some of them are widely adopted in Readify, some others may have been specific to some clients or Readify teams I worked with. Every team is free to choose the tools that work best for them and make it easier to deliver high quality deliverables in sensible time, so, even if you are at Readify, your mileage may vary…
This blog post is about an ASP.NET MVC workaround we implemented in a previous project. We solved the problem by enforcing using a class that extends one of ASP.NET MVC classes, which in itself created another problem, as new developers joining the project may always use the old class. The solution to this problem was not something that I invented, but it’s also not a very common practice.
So, if you are interested, here’s the entire story…
Detecting Session Timeout In AJAX Requests
We wanted to solve a problem where in an AJAX heavy ASP.NET MVC application, if the user triggers an AJAX action after staying inactive for longer than our application timeout, the call to the controller action, which normally gets a JSON response, would instead get the HTML of the login page.
This is a known issue in ASP.NET (particularly System.Web). A feature that’s on by default is returning a redirect to the login page instead of a HTTP Unauthorized Status code (401). After the redirect the response returned is a successful (HTTP Status Code 200) load of the login page. That means even our Angular.JS error interceptors (or jQuery handlers, etc.) don’t notice there was an error.
A fix for this was turning this feature off. We inherited the Authorize attribute as below:
/// Suppresses Forms Authentication Redirects for Ajax requests
/// so the client side interceptors can handle it.
SuppressFormsAuthenticationRedirect is the property that disables the login redirect. Microsoft set it to false by default so that it’s backwards compatible.
ASP.NET MVC doesn’t recognize AJAX requests through Request.IsAjaxRequest() via Accept header or so. It does via checking X-Requested-With header. Most AJAX-capable frameworks like jQuery and others offer a way to intercept all requests and add extra headers, for example, in that app, we configure Angular.JS to include the header with some code similar to this:
The obvious problem with the previous solution is that we are ignoring The Power Of Defaults. Any other developer who may join the project needs to know that using Authorize is a no-no, and even for old devs (myself included),
it’s very easy to just forget and use Authorize not AuthorizeRedirect just out of habit.
Solving this problem was quite easy though, we added the following test to our Unit Tests project:
I hope the code is self explanatory. We check the web project assembly for all ASP.NET MVC Controllers, then we check the Controllers and all their Action methods for existence of the AuthorizeAttribute. We filter those that use the correct attribute (AuthorizeRedirectAttribute), and we Assert that there are no the no Controllers or action remaining, otherwise, we tell the developer which Controller or Action needs to be fixed, and how to fix it as well.
Room For Improvement
The drawback of this is that our Unit Test project had to reference the ASP.NET MVC assemblies and gets more stuff than most tests should need. We can overcome this by moving our “convention” tests into another project completely, but for this project the conventions were very few and it seemed fine.
Of course the same method can be applied to any other convention you enforce in your project. One obvious example is ensuring all Controllers inherit from a custom base Controller class instead of the ASP.NET MVC class directly. I know people who already do this, as I mentioned in the opening the technique is not new by any means, but it’s worth even more popularity.
Speaking of improvement, the code for this test class was optimized a bit while writing this blog post, there is always room for improvement :)
In case you were reading the code carefully, the IsNullOrEmpty() method I used in assertions is a custom extension method we had in the project, a very simple one as you may expect:
Recently I discovered that there’s no one standard way for AJAX-driven server-side paging in ASP.NET MVC (in Web API, you can expose an IQueryable). For the case in hand, I decided to use PagedList for the server bit of the game.
The PagedList interface looks a bit like this (for demonstration only, real code is a bit different, check its source code for the real stuff):
It provides nice properties for paging, and exposes itself as enumerable and has an indexer. Apart from this snippet, the library also provides an extension method ToPagedList() to apply to any enumerable and allow it to populate the properties from it (by enumerating on it, and by calling the Count() method).
The JSON.NET Serialization Problem
Well, yes, except when you have a custom collection like PagedList, and you want to treat it as an object that has several properties, not as an array. JSON.NET does provide a solution for this actually, all you need to do is apply the [JsonObject] attribute to your class.
Unless you don’t own the source code of this class.
In this case, you need to inherit from it. By doing this, I lose the nice ToPagedList() extention method (because it creates an object of the PagedList class directly), but luckily it does nothing but calling new PagedList() with the parameters we give it, so, we don’t lose much.
Apart from having to copy the constructors to pass parameters to the base ones, have you noticed the extra Items property in there?
That’s because the Subset member it includes is actually a field, not a property, and JSON.NET won’t serialize that by default, I could override it somewhere else, but since I’m fiddling with this here, it made sense to just stick a property to the class.
Bonus: A Bit More On Implementation
In my actual code, I have added the Dynamic Linq NuGet package, and assumed a single property for sorting (which was fair for the situations where I intend to use this), so, I complemented the code above with another class that looks like this:
This allows the controller to instantiate an instance of the SerializablePagedList class, pass it all the way to whatever repository method I have.
Even more, now that I think about it, maybe I should change the return type to SerializablePagedList, to make the Items property visible to the developer (because they’d think it’s magic, and in coding, magic is BAD). I’ll leave this as an exercise for you :)
Final Words / Disclaimer
The motivation behind this post is that I found the problem of serializing PagedList using JSON.NET a challenge and I wanted to help others work it out faster than I did. Is this how I’d recommend doing paging? Well, I don’t know, but if it’s what you choose, I hope that I have saved you some time.
And more importantly, is it good enough to be the defacto standard I mentioned I was after in the beginning of the post? Not really. I think it’s not bad, but definitely not the best. I’d love to see less clever (read: hacky), and more simpler solutions.
Windows 8.1 RTM was meant to be held until the retail version in October, but Microsoft changed their mind and made it available for MSDN & TechNet subscribers to allow developers to test their applications on the RTN version (which has several API changes from the preview). If you have an MSDN subscription, go ahead and check your subscriber downloads.
The versions available as of now are Windows 8.1, Windows 8.1 Pro, and Windows 8.1 N. Windows 8.1 Enterprise might be available in a week or two.
Note that many Microsoft partners, you should only use MSDN downloads for VMs not real machines, and should use the Microsoft Partner Network for the real machine installs. Unfortunately the Partner Network will et Windows 8.1 by October with everyone else.
Note that Windows Server 2012 RTM is also available with the same terms, which makes its availability as a hosting OS still practically on hold for many people, btu it’ll be useful for creating test labs, etc. inside environments that consider deploying it.
You can learn more about Windows Server 2012 RTM from:
To help with application testing in Windows 8.1 without wrapping up Visual Studio 2013 RTM too early, Microsoft has released Visual Studio 2013 RC at the same time, with a go-live license (meaning you can use it for production applications). You don’t need a special MSDN subscription to use the RC, but if you plan to develop Windows Store applications, then you really need to install it on Windows 8.1.
There are many new features in Visual Studio 2013, several were available since the previous Preview, but also many have arrived with the RC, here’s a video and complete listing of what’s new in Visual Studio 2013 Release Candidate:
Please let me know if you find these videos useful. I may not be able to do much about the quality in the short term, so, it’s worth knowing if the videos as-is are helping, or I need to pause until I get better tooling than just my phone camera and a simple webcam.
So, check out all the videos on YouTube, leave comments ion them, and let me know the topics that interest you, which may in the future turn into tutorials I create myself, or suggestions to ask usergroup leaders to look for presenters to talk about.
Well, this shows how to do the compression itself. Depending on how you do ASP.NET, you probably will call it differently.
In my case, I called it manually from an ASP.NET Webforms PageMethod (more on why below), but if you are using ASP.NET MVC for example, you probably want to wrap it in an ActionFilter and apply that to the action you want to compress its output. Let me know in the comments or on twitter if you have a problem implementing it in a particular situation.
IIS Dynamic Compression
IIS 7+ has built in dynamic compression support (compressing output of server-side scripts like ASP.NET, PHP, etc.). It’s not by default because compressing dynamic content means running the compression for every request (because it doesn’t know what the server-side script will generate for every request, the point of using server-side programming is generating dynamic content!).
Static compression on the other side (caching static files like styles and scripts) is on by default because once the static resource is compressed, the compressed version is cached and served for every future request of the same file (unless the file changes of course).
General advice: I’d say if your server side scripts expect to return large text-based content (say, large data, even after paging, etc. like large reports or whatever), always turn dynamic compression on, at least for the pages that expect to return large data sets of text.
In many cases though the majority of large files will be scripts (and possibly images) will be the larger parts though, which are often taken care of (for scripts for example) by IIS static compression or ASP.NET Bundling.
All blogs and news websites provide some sort of aggregation feed, usually RSS or ATOM. This allows users to add the feed URL to their favourite aggregator and stay updated with future stuff when they come. This post shows how to get a URL to subscribe to, and how to get multiple URLs if the site provides multiple formats.
Here is what you get when you run the code against wordpress.com:
A few notes on this approach:
You must have realised the Where check in the code, the library seems to capture any related link in the HTMl, not just syndication links. that’s why we needed to filter them explicitly
Quite often when you have a main site that has different branches, you get more than one feed link, for CNN for example you get different feeds for certain site languages, for wordpress.com you got one for the site itself and another for members of the service. Arguably, this is not always what you want when you add the site to a reader kind of application
As expected, this code is quite slow in debug mode, takes about 1.5 seconds to run alone! In release mode (build configuration set to “Release” and so web,config)
In its simplest form, the syndication discovery is a matter of finding a link tag with a proper rel attribute (typically set to alternative), and a type attribute holding the attribute, however, in real life, at least historically, there used to be many variations of the way the discovery was implemented (read the next section for more).
One of those who managed to get right URLs for different edge cases was Google Reader. Apart from Google Reader itself, whose closing was part of the reason I wrote this post, Google allows you to use their systems to get the right syndication feed URL of a given page via simply calling a public JSONP API.
This is the structure of the data parameter returned by the previous call:
Even though social media has made people depend on links shared on social media sites (by their peers, or the creators of the feed), the trend of adding a syndication feed to website is a trend that continued to increase in many product and subscription websites, especially that it’s easy to automate social media posts from the feeds after that.
As Google Reader will be retired in July, I thought for a minute about what it’d take to put some web based reader together. This was before I learned about the existing awesome alternatives like Feedly and so many others.
Then I remembered there was an application I was working on in 2007, one feature we needed and a colleague worked on was getting RSS posts from personal blogs of the site members. I remember seeing him doing all sorts of crazy Regular Expression matches of so many formats to get the URL. Turns out at least at this time different blog providers used different ways to advertise the feed URL in blog homepage markup, there were so many cases, it took my colleague several days to cover a large set of test cases from different providers that we knew our users were using.
I wanted to see whether this problem was still a thing n 2013, and tried to see what options we had, hence came this post, you know, just for fun :).
Hope some of you were interested in this too! CodeProject
If are using the great Web Essentials Visual Studio extension, and you updated the extension yesterday and noticed in Web Essentials change log that several editors like LESS and CoffeeScript editors were removed, this was as the editors moved into the official update.
This means if you want to use the recent update with Web Essentials (strongly recommended), you probably should also update Web Essentials to the latest version first before installing the update (Scott mentions that as well).
Which does the job of matching “/” to HcomeController‘s Index action.
The reason it works is simple, the id parameter is obviously optional, the action parameter if not present will be set to Index, so, this routing will match “/home/index/”, and similarly will match “/home/”. But the controller also will be set to HcomeController if it’s not present (the Controller suffix is a convention in class name), so, this matches “/home/” and “/”.
Using Explicit Route
Until here, there is no point of this blog post. So, lets say for whatever reason you are using other routing rules, because your desired routes don’t follow this very convention or because you don’t want to couple public URL structure to internal class names or have another reason to make your routes explicit, how do you set a route for the homepage or website root “~/” URL in this case?
One possible case is to have this exact default route still there, but make sure it’s at the very end of your route registrations, after all other have been registered, so, it has the least priority in matching. The drawback is that it’s still more generic than its purpose, technically allows using it for other actions than the one handling the homepage, and every request to homepage will go through all routes tried first (which is usually not a very slow thing to be honest).
So, a nicer way is to be able to have an explicit homepage route. Maybe even one that can be put on the top of route registrations to make the request to our homepage the fastest ever (although again, it’s not a big deal or difference, but nice to have). Turns out the way is VERY easy, just set the URL to an empty string “”.
Yeah, that’s it (the “name” doesn’t matter BTW, and you can obviously use whatever controller/action too). Even if you are using the default routes, you can combine it with them, and put it before them too. Empty string will NOT match any URL that is not empty (you can argue all other requests are matched against this, but comparing a string against an empty one must be VERY quick, right?)
Note that this all is application specific, so, the root here is website root “~/” not server root, so, this should still work if your website is hosted under some virtual directory too.
p>Not that it matters a lot, but thought some of you would be interested :).