Contents tagged with MVC3
Intro to ASP.NET MVC3 for Public Sector Developers
Parts.Common.Body.Summary.cshtml – The template for summary of a content item's body.Here are my slides from my webcast the other week on ASP.NET MVC3 for Public Sector developers.
Intro to MVC 3 for Government DevelopersView more presentations from Frank La Vigne
Building Your Own Link Redirector with ASP.NET MVC
Parts.Common.Body.Summary.cshtml – The template for summary of a content item's body.Once you create a Bit.ly link, you can’t change it – ever. A bit.ly url is written in virtual stone and can never be altered.
What if you wanted to great a short url at a moving target?
Not too long ago, some tracking URLs for work changed.
This was a problem, since I created catchy short URLs with bit.ly, shared them out and even printed up cards with those URLs.
Although I do understand their reasoning, I was not happy with the “never change” model of bit.ly.
This got me thinking about writing my own URL redirector and during an extended layover in an airport, I had the chance to write one. Keep in mind, this code is simple and not intended to be a bit.ly replacement. There is no tracking of URLs clicked or any kind of reporting. I also hard coded the URLs rather than loading them from a configuration file, since these links will change annually or semi-annually and I only had a four hour layover.

Redirecting Links
The nice thing about bit.ly is that you can take a long link like http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200090962 and turn it into something short and meaningful like: http://bit.ly/Win8Tools
I liked that feature and wanted to emulate that. Remember, I’m hard coding these links and will rarely have more than five or six at one time.
The first thing was to add a custom route in my Global.asax.cs file:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{urlCode}", // URL with parameters new { controller = "Home", action = "Index", urlCode = UrlParameter.Optional } // Parameter defaults ); }
The above code adds a “greedy" route that will take over every request that comes in, which passes the first parameter into my Index method in my Home controller.
public ActionResult Index(string urlCode) { string destinationUri = DetermineUri(urlCode); Response.Redirect(destinationUri); ViewBag.Message = destinationUri; return View(); }
In my case, I’m deploying this to it’s own directory onto FranksWorld.com and I don’t mind if the route takes over everything. In fact, that’s what I want it to do.
Given a set of constants:
public const string AZURE_SDK = "http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200083057"; public const string AZURE_FREE_TRIAL = "http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200083060"; public const string PHONE_SDK = "http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200083063"; public const string WEB_MATRIX = "http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200083066";
I parse the parameter like this:
private string ExtractLink(string lowerCaseUrlCode) { string defaultLink = "http://www.franksworld.com/"; if (lowerCaseUrlCode.Contains("matrix")) { return WEB_MATRIX; } if (lowerCaseUrlCode.Contains("90")) { return AZURE_FREE_TRIAL; } if (lowerCaseUrlCode.Contains("azure")) { return AZURE_SDK; } if (lowerCaseUrlCode.Contains("phone")) { return PHONE_SDK; } return defaultLink; }
So all of these point to my WebMatrix link.
- http://www.franksworld.com/Go/TryWebMatrixItsFree
- http://www.franksworld.com/Go/WhatIsWebMatrix
- http://www.franksworld.com/Go/Matrix
- http://www.franksworld.com/Go/WhatInTheWorldIsWebMatrix
If the links change, just change the destination link in the constants. I was about to drop that into the web.config file, but that’s when my flight started boarding.