bib.ly – ASP.NET MVC Url Shortener for Bible References

Last week, I turned a URL shortening website called bib.ly which is a play on the word “Bible” and the popular URL shortener bit.ly. It has two main functions: A Bible URL shortener and a Bible version popup detector for any website.

1. URL Shortener

bib.ly let’s you easily create short URLs to Bible verses for use on Twitter and Facebook. You can use the interface you see below or just type in something that looks like a Bible verse after http://bib.ly/

When someone clicks a link, they see the Biblical text and get links to several Bible

2. Bible Refernece Popup

You can also add bib.ly to a site by adding the following two lines of code to your own site

<script src="http://code.bib.ly/bibly.min.js"></script>
<link href="http://code.bib.ly/bibly.min.css" rel="stylesheet" />

This script will find all Bible references like these Mark 5:36; John 3:16; Eph 2:8-9 and create links to the main bib.ly site along with a popup so you can read the text.

How I Built It

I chose to use ASP.NET MVC 3.0 because it makes this kind of URL handling pretty easy and because I wanted the Bible parsing code to look pretty close in both JavaScript and the server-side language (C#). I fooled around with doing the site in node.js, but decided that I wasn’t confident enough in my node.js skills to pull it off and get it properly hosted.
So what follows are my notes about how the site works and how ASP.NET MVC made it really easy to build.

URL design

The site needs to have two kinds of URLs:

  1. Fixed pages – I needed a few normal pages like Home, Plugins, and Contact that will look like this:
    http://bib.ly/plugins
    http://bib.ly/contact
  2. Parsed URLs – All the other URLs will need to be checked to see if they are a valid Bible Reference. Bit.ly has to check if the URL is a valid id in its enormous database, but we need to actually attempt to read the URL and see if it makes sense as a Bible verse.
    http://bib.ly/John3.16 for John 3:16
    http://bib.ly/Gen12:1-3 for Genesis 12:1-3
    http://bib.ly/1Chr11.22-ESV for 1 Chronicles 11:22 in the ESV
    http://bib.ly/asdfasd12 needs to throw a 404 error.

Controller Setup

Getting this to work was actually really easy once I figured it out. Basically, I hard coded the paths to the static pages in one controller and then setup another controller as a wildcard URL handler.

Global.asax

To handle the Bible references, I used wildcard mapping. The only problem with that is that it breaks the normal “{controller}/{action}/{id}” which usually hooks up a root/home page. This means I needed to manually hook up the “Fixed pages” above.

// Map the home page
routes.MapRoute(
     "home", "", new { controller = "Home", action = "Index"}
);
// Map all sub pages
foreach(String route in new string[] { "About", "Contact", "Plugins", "Feedback" }) {
     routes.MapRoute(
           "page_" + route, route.ToLower(),
           new { controller = "Home", action = route }
     );
}
// Send everything else to the Reference controller to parse
routes.MapRoute(
     "verses", "{*referencetext}",
     new { controller = "Verses", action = "Show"}
);

HomeController.cs

This one is pretty boring. It’s just a list of Views and one post handler for the Contact form.

VersesController.cs

This controller has two actions: Show and NotFound. The Show action attempts to parse the URL into a Bible reference. If it’s successful, it shows a page. If not, you get a 404 error page. This could have been handled in the Global.asax with a UrlConstraint, but I thought this was clearer.

public ActionResult Show(string referencetext) {
     // parse the reference
     Reference reference = ReferenceParser.Parse(referencetext);
     // if it didn’t parse, show the not found View
     if (reference == null) {
           Response.StatusCode = 404;
          return View("NotFound");
     }
     // show the view, using the reference object as the model
     return View(reference);
}
public ActionResult NotFound() {
     Response.StatusCode = 404;
     return View();
}

URLs with a Colon (:) Character

Technically, you’re not supposed to use the colon character in a URL without escaping it. But it’s very common to use a colon to separate a chapter and a verse when giving a Bible reference (2 Cor 5:17; Rev. 22:1-4) so I wanted bib.ly to understand the colon character if someone chose to use it.
ASP.NET, however, disallows the colon character (and five other no-nos) by default. To allow colons, you just have to edit the following line in the web.config file.
Default requestPathInvalidCharacters value

<httpRuntime requestPathInvalidCharacters="&lt;,&gt;,*,:,%,&amp;,\"/>

With the colon removed.

<httpRuntime requestPathInvalidCharacters="&lt;,&gt;,*,:,%,&amp;,\"/>

Wrap Up

Hope that gives a basic sense of how the site works. There is a little more code on the C# side to help manage the versions and Bible website links, but the rest of the site’s functionality is mostly just simple HTML and CSS controlled by JavaScript utilizing JSONP to get the Biblical text from their content providers.

11 thoughts on “bib.ly – ASP.NET MVC Url Shortener for Bible References

  1. @jitendra, yes you can easily add it to your site by just including the script tag above in the header of your site. You can use BlogEngine’s advanced settings to add it in the same place where you add analytics code.

  2. You rock. That is so cool and such a fantastic idea. Thank you for making this, I’m starting to spread the word.
    P.S. Your final code line “With the colon removed.” still has the colon in it. 😉

  3. please support local languages as youversion does.
    so we can show bible verses in our own languages.
    tnx!

  4. An interesting discussion is worth comment.
    I do believe that you should publish more about this issue, it may not be a taboo matter but
    usually people don’t talk about such topics.
    To the next! Cheers!!

  5. Do you mind if I quote a few of your posts as long as I
    provide credit and sources back to your webpage? My blog is in the very same area of
    interest as yours and my visitors would truly benefit from some of the information yyou present here.
    Please let me know if this okay with you. Regards!

  6. Máte nízké sebevědomí ɑ myslíte sі, že nemáte na to, abyste sii na internetfu našli sexy protejšek ppro sex v
    Brně na jednu noc, ale tɑ představa vám nedává spát.
    Nikdo neríká, žе máte hledat jen jednoho sexy partnera – známosti na Seznamce neznají mezí
    ɑ jestli se na to cítíte, domluvte sі treba sex vve trech.
    Singles z Ostravy а celé Ceské republiky se obracejí na seznamky а podobné
    služby, protože mají príliš rušný život na tο, aby nekoho hledai v
    гeálném živote.

Comments are closed.