Category: Development

Redirecting MVC controller to HTTPS if required

Check out this excellent blog on how to create a simple ActionFilterAttribute that handles redirecting to https:// if required.

http://weblogs.asp.net/dwahlin/archive/2009/08/25/requiring-ssl-for-asp-net-mvc-controllers.aspx

then just decorate your whole Controller ot ActionResult method with

[RequiresSSL]
 public class AccountController : Controller
 {
 ...
 }

Getting AddThis.NET for BlogEngine.NET extension working!

There are currently a few hurdles to getting the very useful AddThis.net version 5 extension going on BlogEngine.net version 2.5 or later. I have consolidated the steps here.

There are a few ways of getting an extension added to a BlogEngine.Net project. I decided to use the NuGet approach. If you haven’t got this Visual Studio extension installed yet…do it now! see my earlier blog How to easily add open source libraries to net projects using NuGet).

First Configure NuGet

  1. Right-click on your web project and select ‘Manage NuGet Packages…’
  2. This will display the NuGet Manager. Click on the ‘Settings’ button at the bottom left.
  3. This opens the Visual Studio Option dialog. Make sure that ‘Package Sources’ is selected in the tree on the left.
  4. Enter the following and click the ‘Add’ button and then ‘OK’ on the Options dialog to close and save the new settings.
  5. You should now see the new BlogEngine.Net extensions library list under the ‘Online’ section. If necessary close the NuGet Manager and re-open it from the projects shortcut menu again.
  6. Simply select the extension you like and click ‘Install’. Nice an easy!

Second – Fix the project!

You will now try and rebuild your project to find that a whole load of failures occur.

This is easily fixed.

You will need to add the following reference to /App_Code/Extensions/AddThis.cs and pretty much every file under /App_Code/Extensions/BookmarkButtons, but the errors panel will give you the list of issues.

using BlogEngine.Core.Web.Extensions;

Then rebuild and voila…hopefully no errors!

Refresh a controls binding in Silverlight or WPF without using PropertyChanged

I often need to change the Language (or culture) of a control (changing the Control.Language) or the whole thread (changing CurrentThread.CurrentUICulture). Such as when displaying dates and currencies in a particular culture. Anyone who has tried this will realise that the controls will not reflect the change until an underlying value has updated.

Alternatively you can rebind the control. To help I have created a simple generic method to help.

        
    ///<summary>
    /// Rebinds a controls dependency property to it's original binding. Used to refresh a control.
    /// </summary>
    /// <remarks>Can be used after updating a controls Culture</remarks>
    /// <param name="cntrl">The control to which the dependency property exists</param>
    /// <param name="depprop">The dependency property of the control</param>
    public static void ForceControlRebind(Control cntrl, DependencyProperty depprop)
        {
            try
            {
                BindingExpression BindExp = cntrl.GetBindingExpression(depprop);
                Binding Bind = BindExp.ParentBinding;
                cntrl.SetBinding(depprop, Bind);
            }
            catch (Exception)
            {
                throw;
            }
        }

To rebind a control just pass the Control and the DependencyProperty whose binding you wish to refresh.

So, for a TextBox called txtBox1 the syntax would be:

ForceControlRebind(txtBox1, TextBox.TextProperty);

Interactive demo app of Windows Phone 7 for Android and iPhone

Microsoft have rather cleverly created a little demo web app that lets an android or iPhone user get some idea of how the WP7 phone works.

Check it out at http://m.microsoft.com/windowsphone/en-us/demo/

Start showing people how much better WP7 clearly is as a phone OS.

A bit bizarre, but it does not work in my IE9 browser!!

Understanding CA2214 code analysis warnings using ViewModels

If like me you didn’t understand the the rest of the message and it’s implications here is a simple explanation and answer.

When an object that implements an inherited class (e.g. a ViewModel that inherits from ViewModelBase) is constructed, the constructors in the base class or classes are executed first.

However as the base classes execute methods or events, if these implemented methods or events are overridden, by a more derived class, then the base could call these and not it’s intended base methods. This could cause some unexpected behaviour.

If you make calls to potentially override-able methods yourself (e.g. RaisePropertyChanged) , you probably intended to and know full well what the consequences are. So why the annoying message?

Well, CA2214 is warning you that this may become a problem if anyone decides to inherit from your class (e.g. your ViewModel). As this is probably not something that you want or intend in your project, you can eliminate this warning by simply sealing your class!

    public sealed class MainViewModel : ViewModelBase

This will ensure that your class cannot be inherited from. This should make the warning go away.

If you are writing your own base class or a class that is to be inherited from, then don’t call override-able methods in your constructors!

Hope that helps! hhhmmmmm

How to easily add open source libraries to .NET projects using NuGet

I have just come across one of the best Visual Studio extensions called NuGet (http://nuget.org/).

It makes it incredibly easy to add available libraries, including such things as JavaScript libraries like jQuery. Not only that, but it will work out any dependencies, download those and then warn you if any get updated.

The easiest way to get going is to add it to Visual Studio using the ‘Extension Manager’ which you will find under the ‘Tools’ menu. It’s called ‘NuGet Package Manager’.

Once installed, it is simplicity itself to add a reference and download the binaries, dependencies and code for a useful library (such as MVVM Light or jQuery).

  1. Right-click on the references folder in your solution.
  2. Select ‘Manage NuGet Packages…’image
  3. and voila…search for new libraries or check for updates. If NuGet finds any updates then you will get an alert when VS2010 starts.

SNAGHTML198fc1

Potential TFS connection Problem TF30063

There is one fly in this ointment that took me a little time to resolve. A bug in NuGet can force the connection to your TFS server to be dropped. The only way out is to restart Visual Studio.  the solution to this turns out to be extremely trivial.

All you need to do is add your TFS server URL to the Intranet security group in Internet Explorer!

The credit for resolving this must go to the very brilliant effort by Miha Markic. See his blog about it at http://blog.rthand.com/post/2011/08/26/Fixing-combination-of-NuGet-and-Team-Foundation-in-workgroup-configuration-401-Unauthorized.aspx