Bring Rails to ASP.NET MVC With Restful Routing

Restful Routing - Url

When I’m working with ASP.NET MVC, I find myself wanting to do things “the Rails way.” There are a lot of excellent conceptions/conventions in Rails where I think ASP.NET MVC could really learn something.

One thing I really dislike about ASP.NET MVC is the way it handles routing. I find the default route in ASP.NET MVC ({controller}/{action}/{id}) to be very ugly, antiquated, and super greedy. When I’m writing an application I like the URLs to be “hackable.” This isn’t possible with the default ASP.NET MVC routes. Take, for example, something like a book website. Using the default route, you would have something like /books/edit/1. Now what happens if we remove that one (the id)? Boom 404! What about the default route for a single book? You would have /books/details/1. You know what happens if we remove that id. The URLs just do not flow nicely.

Routing Approaches

Why do I prefer Rails routing? Their approach makes so much sense to me, not to mention that you get nice restful routes. The approach that Rails takes is called resource-based routing.

ASIDE: What is a resource you ask? You can think of a resource as a model, and with a model, you would typically want CRUD operations. That’s where the “routing” comes into play. Instead of using a greedy route, it sets up specific routes to handle the CRUD operations for a given resource. We’ll be taking a closer look at this as we continue, but if you’re eager to learn more, head over to the Rails Guides on Resource Routing.

With this resource approach, the URLs follow RESTful conventions and embrace HTTP. Isn’t that one of the key concepts behind the MVC pattern, embracing HTTP?

Let me show you an example of the difference in approaches.

We’ll base the comparison on the basic seven actions that are required to perform CRUD operations (assuming you have a New and Edit action to handle an HTML form, otherwise you would only need five).

The ASP.NET MVC Approach

Action HTTP Method URL
Index GET /books
Create GET /books/create
Create POST /books/create
Details GET /books/details/1
Edit GET /books/edit/1
Edit POST /books/edit/1
Delete POST /books/delete/1

Notice how only HTTP GET and POST are being used? HTTP has more verbs than just those two. Why not use some of these to clean up our URLs and be RESTful? Continue reading

We Love Books!

We Love Books And We Know You Do As Well…

DevBookcase Logo

As you may or may not know, Visoft has quietly released websites for book lovers. Back in 2010, I fell in love with Ruby on Rails. The first Rails application that I worked on was one to help me keep track of my ever growing book collection. Thus the first book related website was launched, Developer’s Bookcase or DevBookcase for short. As you may guess, my book collection is mainly programming related, so I started the site for me and other geeks to keep track of our programming books. I have hundreds, how about you?

Why So Quiet?

Back in 2010, I was really just hacking around. I was really venturing into an entirely new world. Ruby, Rails, Linux, OS X, etc. If you know me, you’re probably wondering about the Linux and OS X there. Yes, I’ve really made a big shift in things, but that’s for another post. Anyway, at that point, the site was really just a big test. I kept hacking away adding things here and there, experimenting with this and that. You may have seen posts that indicated that I was playing more and more with Rails, like my gem issue, and my mongrel issue. Those were early on. I was using Rails 2.3.x and I was messing with shared hosting (yuck). Things worked, but I wasn’t confident, mainly because I had to bump my nose a bit to learn the ropes. Anyway, long story short, I got distracted… Continue reading

Streaming Large Files Asynchronously using .NET 4.5

Recently I had a project that required me to be able to transfer large files via a service call to a remote server where the file would be ingested by an always-on application for further processing. When thinking about the requirements for such a service, a few things came to mind:

  1. The transfer had to be streaming, as attempting to buffer requests for files in excess of 100MB would be taxing on the host server.
  2. To maximize throughput, the requests need to be released as fast as possible so that new requests can be processed.
  3. The service should be as simple and quick to build as possible.

After a bit of research on the matter, I struck the jackpot with the HttpTaskAsyncHandler class and the recently introduced GetBufferlessInputStream() method and I soon got to work on mocking up the functionality. Continue reading

Kendo UI Upload Control With Existing Files

kendo-logo
If you hadn’t guessed by my last two posts, I’ve been using Telerik’s Kendo UI controls for a project I’m working on. Today we’re going to talk about the Kendo UI Upload control, which I have been using in asynchronous mode. As I’ve stated plenty of times before, I’m not a huge fan of 3rd party controls, but the Upload control is pretty nice. That said, there is a nagging issue with the control.

The Problem

The project I am working on has a form that users use as an editor for a “forum” of sorts. They can enter messages and add attachments. They are also free to edit their entries, and make changes to the attachments (add or remove them). Well, that’s where the Kendo UI Upload control falls short. There isn’t a “built-in” way to add an existing list of files and let users remove them using the default Upload interface. You could always write your own UI for displaying the existing files, but that’s a bunch of extra work. Shouldn’t you just be able to use the existing interface and not have to come up with something different? That was my goal. You can hack things fairly easily so that you get a list of the files, but the built-in “Remove” button doesn’t work. That’s what we will discuss in this post. We’ll get the list generated AND have the “Remove” button work as expected (where it calls your Remove URL and actually removes the item from the list).

ASIDE: Before we go any further: I’m not saying “Go out and buy Kendo UI” with this post; there’s no incentive in endorsing the product. Of course if Telerik wants to provide me with a free license, you know, that would be cool ;) Again, I’m not a huge fan of 3rd party tools. I happen to be using them for a client and I encountered this specific issue, and I’m not the only one. The purpose of this post (like the majority of the posts on this blog) is to help others keep their sanity.

There have been numerous inquiries around the web regarding this nagging issue, at StackOverflow, the Telerik MVC Controls Forum (the precursor to Kendo UI), and the Kendo UI Forums. Despite this, there are no real solutions on the web, though that MVC Controls Forum post gets you close.

In case you aren’t familiar with asynchronous Upload control, it’s a very simple design.

Kendo UI Upload Control

Pretty simple, right? Figuring out how the Upload control works, lies in hacking around in the HTML and JavaScript source code. Continue reading

Telerik Kendo UI MVC Extensions and Icky HTML

I like clean code. Remember the days of obtrusive JavaScript?

<a href="#" onclick="alert('You clicked!')">Click Me!</a>

Yuck.

Move that click event to a handler attached via JavaScript and that makes for much nicer HTML.

There are other annoyances that I encounter in HTML code beyond obtrusive JavaScript, one that gets to me is <script> tags all over the rendered HTML. Well, that’s exactly what happens when using the Kendo UI MVC Extensions! For example:

<body>
  ...
  <ul class="k-widget k-reset k-header k-menu" id="mainMenu">
    <li class="k-item k-state-highlight k-state-default">
      <a class="k-link" href="/">Home</a>
    </li>
    ...
  </ul>
  <script>
  jQuery(function(){jQuery("#mainMenu").kendoMenu({});});
  </script>
  ... more html ...
</body>

Icky HTML in my opinion. Ok, I’m nitpicking, but here’s one annoyance with this approach. I MUST declare jQuery and the Kendo UI JavaScript at the top of the page. I like my scripts to be nice and tidy at the bottom of the page, like a responsible web designer.

My recommendation for using Kendo UI would be to use it without the MVC helpers. Personally I love JavaScript; I don’t need no stinkin’ helpers :)

UPDATE: It looks like a “DeferredScripts” solution is coming in the Q1 2013 release of the Kendo UI, so this may soon be a moot point.