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

April 1, 2011 | .NET, Bible Tools, JavaScript | 29 Comments

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.

29 Responses to “bib.ly – ASP.NET MVC Url Shortener for Bible References”

  1. Nice post John, Thanks.

    So we can easily integrate it on our site or blog. Any idea for the usage it in blog engine ?

  2. John Dyer says:

    @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.

  3. Jeff Lewis says:

    Awesome! Makes me wish I still had my scripturefeeds.com site running…

  4. Steve Hiner says:

    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. ;-)

  5. dny says:

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

    tnx!

  6. What’s up to every one, as I am genuinely eager of reading this web site’s post to be updated regularly.
    It includes fastidious information.

  7. Howdy! I could have sworn I’ve visited this blog before but after looking at many of the
    posts I realized it’s new to me. Regardless, I’m certainly happy
    I discovered it and I’ll be bookmarking it and checking back frequently!

  8. WOW just what I was looking for. Came here by searching for split air conditioners

  9. Hi there everybody, here every person is sharing these kinds of experience,
    so it’s nice to read this webpage, and I used to pay a visit this weblog daily.

  10. Hi there! Do you use Twitter? I’d like to follow
    you if that would be okay. I’m undoubtedly enjoying your blog and look
    forward to new updates.

  11. Projects like replacement of roofing system require permits, while repairs do
    not. For various reasons ranging from all time low real estate prices to high volumes of capital opportunity, here is a list
    of the top ten cities to start a business today. Green roofs are types of roofs which contain moss, grass, and
    other kinds of plants growing on the surface.

    Review my web page siding or Paint

  12. Dianna says:

    Hello there! Do you use Twitter? I’d like to follow you if that would be okay.
    I’m definitely enjoying your blog and look forward to new posts.

  13. This site really has all of the info I needed about this subject and didn’t know who to ask.

  14. Amazing blog! Is your theme custom made or did you download it from
    somewhere? A theme like yours with a few simple adjustements would really make
    my blog jump out. Please let me know where you got your design. With thanks

  15. Thankfulness to my father who shared with me about this website, this blog is really remarkable.

  16. Hi! I just want to offer you a big thumbbs up forr thee excellent information you havee goot here on this
    post. I’ll be coming back to your web site for more soon.

  17. Hello my loved one! I wish to say that this post is
    awesome, great written and include approximately all important
    infos. I would like to see more posts like this .

  18. Thanks for finally talking about > bib.ly – ASP.NET
    MVC Url Shortener for Bible References | John Dyer’s Code < Loved it!

  19. Congressional Republicans held a hearing on Abound Solar shortly after the company filed for bankruptcy.

    They should do that even if they only suspect medical malpractice, considering tha attorneys
    usually offer an initial free consultation. Eery year all around the pleading world, there are thousands of people get ill, the world is narrowing for a human life, advancement has given fruuts to us bbut
    alo ring us the fears of global warming, pollution, and the ever increasing rate of newer diseases.

  20. It’s amazing in favor of me to have a website,
    which is useful designed for my know-how. thanks
    admin

  21. Tracy says:

    Greetings from Florida! I’m bored to tears at work
    so I decided to check out your website on my iphone during lunch break.

    I enjoy the info you provide here and can’t wait to take a look when I get
    home. I’m surprised at how quick your blog loaded on my phone ..
    I’m not even using WIFI, just 3G .. Anyways, excellent blog!

    my weblog … Codici Xbox Live Gratis (Tracy)

  22. Then after that they can decide to option for a third
    year. Knowing the right keyboard shortcuts will fix this problem.
    Keypad includes the buttons for dual view, home screen and bookmark.

  23. Now I am going away to do my breakfast, after having my breakfast coming over again to read more news.

  24. What’s up i am kavin, its my first occasion to commenting anywhere,
    when i read this post i thought i could also make comment due to
    this brilliant paragraph.

  25. 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!!

  26. 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!

  27. 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.

  28. perceraian says:

    This post is in fact a pleasant one it assists new the web users, who
    are wishing for blogging.

    Look at my webpage; perceraian

Leave a Reply

Hi, I'm John Dyer. In my day job, I build websites and create online seminary software for a seminary in Dallas. I also like to release open source tools including a pretty popular HTML5 video player and build tools that help people find best bible commentaries and do bible study. And just for fun, I also wrote a book on the theology of technology and media.

Fork me on GitHub

Social Widgets powered by AB-WebLog.com.