Unger Technologies LLC


Persistent URL Proxy in C# MVC

I have quite a few websites, blogs and emails that contain links that frequently stale over time because I have a newer version of the document or I want the URL to point to some other resource.

Basically I want a 1 URL that doesn't change and I want to be able to change what that URL is actually pointing to.


In an email, I might have a link to a resume on Google Drive. Google Drive assigns a unique URL to my resume in WORD format when I share it. So now I have a URL pointing to my resume on Google Drive and it looks like this:


I can put that URL into my email and send the email out but what if I create a new file with updated resume information? Maybe I delete that file in my google drive and now anyone who clicks on the link to my resume will come to a resource not found.


Instead of making changes to "all" of the places that have the URL and re-publishing emails, blog posts, web content, I would prefer to just have a table of URL lookups in one place that's easy to update.

In order to solve this problem, I wanted to do something very quickly (like 5 minutes).  I don't want to pay for anything and I want to use resources I already have to put it in place.

With an existing website I operate, www.mywebsite.com, I have created an MVC controller to handle my URL proxying.

My website: www.mywebsite.com
Url to handle translations:  www.mywebsite.com/UrlProxy/UrlRedirector/[KEY]

Example: Url to handle translations:  www.mywebsite.com/UrlProxy/UrlRedirector/MyPhoto1

On my google drive, I have a text file that is comma delimited name/value pairs which are line delimited by carriage return line feeds.

My google text file looks like this:


Then I create a controller to consume my URL lookup data and do the redirecting:

using System;
using System.Linq;
using System.Web.Mvc;

namespace MyControllers
    public class UrlProxyController : Controller
        public void UrlRedirector(string id)
            string fileStoreId = "[ID of Google Docs Text file data store]";
            string baseUrl = "https://docs.google.com/uc?export=download&id=";
            string urlStore = baseUrl + fileStoreId;
            var contents = new System.Net.WebClient().DownloadString(urlStore);

            string[] lines = contents.Split(new char[] {'\r', '\n' });
            string url = String.Empty;
            foreach (string s in lines)
                var keyValue = s.Split(',').ToList();
                string key = keyValue[0].ToString();

                if (key.Equals(id))
                    url = keyValue[1].ToString();

            if (String.IsNullOrEmpty(url))
                Response.Redirect("http://www.google.com", true);
                Response.Redirect(url, true);

Now I want to modify my RouteConfig file because I want as short a URL as possible when using my PURLController action method. I am pretty certain I will never have a path that is named just "p" in my application so I will route all requests to "p" to my PURLController. Now my URL paths will look like this:

My website: www.mywebsite.com
Url to handle translations:  www.mywebsite.com/p/[KEY]

public class RouteConfig
        public static void RegisterRoutes(RouteCollection routes)

                name: "PurlRoute",
                url: "p/{id}",
                defaults: new { controller = "PURL", action = "Url", id = UrlParameter.Optional }

That is all she wrote. Now I can update URLs anytime easily from anywhere. You also can remove them to stale them or you can extend the functionality further by having a third item used to stale the redirect after a certain date time.

i.e. my text file would look like this

SomeIdorKey,http://www.msn.com,2015-12-01 11:59:12AM

Now when you consume this, check the date time value to see if the redirect is still valid otherwise redirect to some notification page saying the redirect is no longer valid.

Let me know if you have any questions.

No comments:

Post a Comment