Author: nrogoff

.Net MVC 3 Logon does not redirect out the box–quick fix!

It seems that the templates for MVC 3 razor are not quite complete. If you attempt to browse to a page where authentication is required, then you will be redirect to the Logon page with a ReturnUrl query string.

Something like:

http://mysite.com/Account/LogOn?ReturnUrl=%2fThePageIWant

That’s great except, out the box, the Logon page does not make use of this and the destination page is not redirected to after a successful login. The fix is easy.

Add the returnUrl route value into the Html.BeginForm method as shown below.

Html.BeginForm("LogOn", "Account", new { returnUrl = Request.QueryString["ReturnUrl"] })

You should already have some code in the controller that does some sanity checking on the returnUrl before doing the redirect. Somthing like:

if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
       && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
    return Redirect(returnUrl);
}

Make sure this is after the SetAuthCookie line!

Cool stuff and Tips in Windows 8.1

Many of the new features and enhancement brought in Microsoft Windows 8.1 can easily go unnoticed and unappreciated. Here is a list of new features that I have discovered and think are worthy of a mention and some tips with the functionality. They are not in any particular order, more in the order in which I discover them and or can be arsed to write about them!

I am publishing this now and will update it as new things are discovered, and there are a lot!

NOTE! Some of these things may have been around in previous versions of Windows, but I have only really discovered now!

 Calculator (New)

 

A little thing, but the new calculator looks great and includes a nice converter too. No need to get another converter app.

The Standard and Scientific options have got the usual options

The converter has almost every options you could want.

 Calendar

This has been improved a touch with the inclusion of a new ‘What’s next’ view and more info cycling on the larger tile.

 Reading List

Reading list is a simple new app that helps you collate things that you want to read later. Maybe like this blog!

You can simply add items to your reading list from compatible Apps such as Internet Explorer (Touch version only) by using the ‘Share’ charms menu.

From within ‘Reading List’ a click on the listed article opens the appropriate app and pops itself into a slim panel as shown below.

Shortcut Keys

I posted this before, but included here again! Here are few handy Windows Key shortcut keys I find useful

Windows logo key+ D Go to desktop
Windows logo key+ W Search settings only
Windows logo key+ F Search files only
Windows logo key+ C Show the ‘charms’ menu (the one on the right)
Windows logo key+ Tab Switch between recently used apps and programs (left apps bar)
Windows logo key+ L Lock the screen
Windows logo key+ E Opens windows explorer
Windows logo key+ R Open the Run dialog
Windows logo key+ S Opens the Search everywhere. Also do this by pressing the Windows key and just start typing
Windows logo key+ X This opens the ‘Quick Link’ menu. Very useful for the power users out there.
Windows logo key+ SHIFT + (left or right arrow) Move an app or window from one monitor to the other if you have multiple monitor.

For a full list of Windows 8 shortcuts see http://windows.microsoft.com/en-gb/windows-8/keyboard-shortcuts

 Windows Journal

This is an interesting little app that can be very useful. Unless you have the pro version of Adobe Acrobat this app allows you to mark-up, annotate and generally scribble all over any document or thing you can print. When you first start Windows Journal you are asked if you would like to install the printer driver (Journal Note Writer!), say yes. If you didn’t you can do it later from the menus.

Once your doc is there then you can draw and highlight to your hearts content. Unfortunately it does not have a good variety of export or save options. Although you could print to xps or, if you have a pdf printer driver, to that.

You can send by email with these options too (the best being .mht)

 Maths Input Panel

This is for the Maths student or geeks out there. In word or other apps, it can get extremely fiddly writing complex equations.

This is the Word equation toolbar!!!

This little app could help you if you have touch screen and just want to try and write your formula and have it work out how to make it pretty and in a way that can insert into your lovely document.

Open the app, write your crazy formula and click insert. It’s recognition is not perfect, but it can help. Note, not all apps can take the output from this app. I have only tried word and Windows Live Writer. Live writer did not understand. The output seems to be the same as the Word Equation writer, so after inserting into word, you can continue with the standard tools.

Shutdown or Restarting Windows 8 in a Remote Desktop Session

In a remote session the normal shutdown button only allows you to disconnect or log off. After a bit of research I found 2 easy ways of restarting a remote desktop session.

  1. Go to the Desktop
  2. Close all open desktop apps
  3. Type ALT+F4 (this is the shortcut for closing apps and programs too)
  4. Up pop a new ‘Shut Down Windows” Windows 8 dialogue
  5. Then select the command of you desire.

If you are commend line oriented then,

  1. Type Windows logo key+ R to open the ‘Run’ dialog
  2. Type either ‘shutdown /r’ for a restart or ‘shutdown /s’ for a simple shutdown and click OK.
  3. If you want to see a UI or do this remotely, you can always try ‘shutdown /i’ on your local machine and do a remote shutdown.

Handy Windows 8 and 8.1 Shortcut Keys

Shortcut Keys

Here are few handy Windows Key shortcut keys I find useful

Windows logo key+ D Go to desktop
Windows logo key+ W Search settings only
Windows logo key+ F Search files only
Windows logo key+ C Show the ‘charms’ menu (the one on the right)
Windows logo key+ Tab Switch between recently used apps and programs (left apps bar)
Windows logo key+ L Lock the screen
Windows logo key+ E Opens windows explorer
Windows logo key+ R Open the Run dialog
Windows logo key+ S Opens the Search everywhere. Also do this by pressing the Windows key and just start typing
Windows logo key+ X This opens the ‘Quick Link’ menu. Very useful for the power users out there.
Windows logo key+ SHIFT + (left or right arrow) Move an app or window from one monitor to the other if you have multiple monitor.

For a full list of Windows 8 shortcuts see http://windows.microsoft.com/en-gb/windows-8/keyboard-shortcuts

How to get ‘Create Unit Test’ shortcut back in Visual Studio 2013

Why the hell Microsoft thought it was a good idea to remove this I have no idea. If they want good reliable code on their platforms you would have thought this would be good to leave in to encourage developers to create unit tests!

Anyway, rant over, this blog explains all the options. The last one being the first one to try!

http://serena-yeoh.blogspot.com.es/2013/02/visual-studio-2012-create-unit-test.html

TimeSpan to String Converter for XAML binding

Need to convert a TimeSpan to a sensible string for you Windows Phone or Windows Store App. With a little Bing help I created this converter. I hope you find it useful.


public class TimeSpanToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        TimeSpan span = (TimeSpan)value;
        if (span == TimeSpan.MinValue)
        {
            return "0 seconds";
        }
        string formatted = string.Format("{0}{1}{2}{3}",
            span.Duration().Days > 0 ? string.Format("{0:0} day{1}, ", span.Days, span.Days == 1 ? String.Empty : "s") : string.Empty,
            span.Duration().Hours > 0 ? string.Format("{0:0} hour{1}, ", span.Hours, span.Hours == 1 ? String.Empty : "s") : string.Empty,
            span.Duration().Minutes > 0 ? string.Format("{0:0} minute{1}, ", span.Minutes, span.Minutes == 1 ? String.Empty : "s") : string.Empty,
            span.Duration().Seconds > 0 ? string.Format("{0:0} second{1}", span.Seconds, span.Seconds == 1 ? String.Empty : "s") : string.Empty);
        if (formatted.EndsWith(", ")) formatted = formatted.Substring(0, formatted.Length - 2);
        if (string.IsNullOrEmpty(formatted)) formatted = "0 seconds";
            return formatted;
    }
        
    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        string strValue = value as string;
        TimeSpan resultSpan;
        if (TimeSpan.TryParse(strValue, out resultSpan))
        {
            return resultSpan;
        }
        throw new Exception("Unable to convert string to date time");
    }
 }