Tip of the day: Random sort a List of objects with Linq

Let’s say you have a list of objects and you want a random presentation to your website visitor. Below is a simple, but effective random algorithm in C# with Linq-to-object by using the GUID type (see line 10 below).

   1:  List<MemorySearchResult> list = Session["MemoryList"] as List<MemorySearchResult>;
   2:              
   3:  // see if list is already in user session
   4:  if (list == null)
   5:  {
   6:      // list not in session, so create a new one
   7:      var newList = myRepository.GetListOfObjects();
   8:   
   9:      // randommize
  10:      list = newList.OrderBy(tempGuid => Guid.NewGuid()).ToList();
  11:   
  12:      // store in session
  13:      Session["MemoryList"] = list;
  14:  }
  15:   
  16:  // now list is randomized and stored in session and ready to use

 

Easy does it!

Disable auto-update for Chrome extensions

Sometimes a Chrome extension gets very popular and then the authors choose a commercial path. Or some feature is discontinued, but you want to keep it as is. Below I describe a way to keep the extension from auto updating itself with the help of the Chrome extension source viewer. This extension is very useful for learning how to develop chrome extensions (how did they did this/that?).

First install the Chrome extension source viewer from the Chrome webstore. Or if you are planning to use it once, you can use the online version.

image 

Steps to modify a Chrome extension (to disable auto-update)

  1. Download a specific version of a Chrome extension. My example I used it for is Media Hint. Version 0.1.12 can be found here

  2. Go to your extensions folder in Chrome, turn on Developer Mode, and click on the options link below Chrome extension source viewer

  3. Open the viewer

  4. Open the recently downloaded Media Hint file

  5. Click "Download" in the upper left corner. Finder will open showing you a null folder that contains the Media Hint logo, a javascript file, and the manifest.

  6. Open the Manifest JSON file (Any text editor will do)

  7. Change the update url (found in quotations) to http://127.0.0.1 (you own local machine IP) and save the file

  8. Optional: Go to the Chrome Developer tools and choose to pack a chrome extension to create a .crx file again.

  9. Drag the entire null folder or the .crx file (with the newly edited manifest file) into your Chrome Extensions page with developer mode turned on.

  10. . Enjoy your customized extension

Hope this helps,

Guus

Tip of the day – redirecting in Asp.net

When you want to disable a specific site, there are several ways to do this. One is to remove the site completely or disable the site in IIS. But then the visitor of the site will get a 404 not found message. When you have a new site you want to redirect to, you can use this little snippet in your web.config to redirect to any url / site:
 
..
<location path="index.aspx">
    <system.webServer>
      <httpRedirect enabled="true" destination="http://www.other-site.com/" httpResponseStatus="Permanent" />
    </system.webServer>
</location>
..
 

Sidewaffle templates

Sidewaffle is a Visual Studio extension that gives you more templating power. It has lots of snippets for Item templates and Project templates. You can install it via the ‘Extensions and Updates’ or by downloading the .vsix package manually. Did I already mention that is productivity tools is coming from Microsoft and is open source on GitHub?

Install:

image

New project templates are visible under each project type. Note: screenshot is created on 13 april. It’s open source, so new templates can be there after each update (notification via the standard Visual Studio window).

image

After selecting a template, a default setup is ready to go:

image

Let say you want to create a new Robots.txt. You can choose it as a template:

image

Use AngularJS template for controllers, directives, etc (with/without typescript):

image

Example code for a AngularJS controller template:

(function () {
    'use strict';
 
    var controllerId = 'controller1';
 
    // TODO: replace app with your module name
    angular.module('app').controller(controllerId,
        ['$scope', controller1]);
 
    function controller1($scope) {
        $scope.title = 'controller1';
        $scope.activate = activate;
 
        function activate() { }
    }
})();

 

Hope you now have a good overview of what SideWaflle is and how it can help you speed up your development.