Introducing CodePaste.NET
I’ve recently put up my first MVC project online: CodePaste.NET, which is a public code pasting site that allows you to paste and link code from social network sites like Twitter and Facebook or chat and IM applications like Skype or Messenger. The idea is that these social network or chat clients work well for interactive discussion but they don’t allow enough space to post even the shortest snippets of code for all participants to see. As a developer a lot of my Twitter traffic deals with discussions around code or code concepts so a place to share and view code is very useful.
So rather than squeezing code snippets into these clients you can post it on the the CodePaste.NET site and then link to the snippet.
To be clear this isn’t an original idea. The conceptual credit for this idea goes to pastie.org and several other sites that provide similar basic services. In fact, I’d been using pastie.org for some time for posting and linking code snippets and most of the ideas of the CodePaste site are directly derived from the base concept of pastie.org. However, CodePaste.NET is a little more specific to .NET developers, providing syntax highlighting and the ability to apply comments on snippets and few other features planned for later releases.
How does it work?
In case you haven’t used any of the code pasting sites, lets take a look and see how the process works.
- You create a new Code Snippet by entering your code and giving it a title
- You ‘paste’ it
- You capture the link from the Address Bar
- You reference the link in a Social Network client
- Other people can click the link and view your pasted code
- Discussion ensues! (hopefully)
To create a new code snippet just takes a few seconds using a simple input form:
Ok maybe it doesn’t look quite so simple, but only the title and code fields are required if you’re in a hurry. The rest of the fields are optional. Selecting a language is useful for syntax highlighting and you can apply a comma delimited list of tag strings so the code can be cross referenced (at some point when there’s better search support ;-}).
Common left margins are automatically cleared from the code you paste so the code will left align properly even if you cut out of the middle of a nested code file which I find very helpful (not surprisingly, eh?). You can optionally specify that your snippet should show line numbers by default although you you can also manually toggle those on and off on the actual code display page.
Once you’ve ‘pasted’ your code you end up with a linkable page that looks something like this:
The link looks something like this:
which can pick out of the Address bad and then easily embed into social network clients as a URL link. Codepaste URLs are short so they take up minimal space in tweets even without URL truncation.
Below you can see a code link to CodePaste.NET linked from a Twitter tweet and a Skype chat:
The links point at the snippet page which displays code in a number of syntax color highlighting formats. From this page you can paste code to the clipboard and toggle line numbers via the toolbar options just above the code or you can manually select and grab the code from the HTML.
If you are signed in and on your own snippet you can also edit/update the snippet, language, tags and original code comment entered inline of the page. Signed in users can also create comments on their own or other user’s code snippets. Sign in is totally optional – you can paste code without being signed to make it as quick and easy as possible to get code posted and linked. Signing in allows for seeing your snippets in context as well as the ability to update your snippets and posting comments to other snippets.
The viewing options available currently are pretty slim with only a Recent and My Snippet list views. The following is the Recent View:
that lets you see 30 or so of the most recent snippets. In the future there will be additional list views for searching and friend views.
API Returns
The individual snippet view and any of the list views can also return data in JSON, XML, RSS or ATOM formats by following any of the urls with ?format=json (or xml,rss,atom). Here are a few examples:
http://codepaste.net/list/tag/MVC?format=xml
http://codepaste.net/x5ndfj?format=json
http://codepaste.net/recent?format=rss
You can append the ?format= to most URLs and get an API result of the data. Rss and Atom only work on list data not on individual snippets. Eventually there will also be a more formal API to retrieve code snippets with an API controller (only stubbed at this point).
The main RSS feed can be accessed via Feedburner and links to the recent snippet view:
http://feeds.feedburner.com/codepaste
The feed doesn’t include syntax highlighting at the moment as the highlighting relies on a stylesheet not accessible in the feed.
Useful?
This project has been a side project I used for taking ASP.NET MVC for a spin. It’s been a learning experience and while it took a bit of adjustment the learning process and implementation was easier than I would have thought. The site is a work in process and I’ll be talking more about my experience with MVC from the perspective of the WebForms mindset.
The site is a work in process and I’ve put the code for this project into a public Subversion repository at:
http://www.west-wind.com:8080/svn/codepaste/trunk/
so if you want to have a look or play with this yourself have a look. Be gentle with your criticism though – it’s my first MVC app and I’m sure the MVC diehards will have lots of issues with my implementation or even overall philosophy. But I welcome some discussion and ideas. If you are interested in contributing or providing feedback you can do so on our message board:
http://www.west-wind.com/wwThreads/default.asp?forum=CodePaste.net
Primarily though I built this as a tool to post and discuss code myself, so if nothing else it’s useful to me. I hope some of you will find it useful as well and the site is available for you to use as you see fit. I’m hoping to evolve this site a bit – I think there are lots of cool things that can be done with this as a code snippet repository.
Anyway, stop by the site and check it out…
The Voices of Reason
# re: Introducing CodePaste.NET
One thing I would like to see is a checkbox that prevents my snippets from appearing in 'Recent Snippets'...
# re: Introducing CodePaste.NET
I'm just starting out with mvc myself. I snagged your code off of svn and am checking it out. I'm most interested in the way you support multiple return formats based on the parameter a user has in the url. Could you (or anybody) direct me to where in the code that functionality is located, or any more information about doing that?
Thanks.
# re: Introducing CodePaste.NET
# re: Introducing CodePaste.NET
# re: Introducing CodePaste.NET
Any result that wants to participate in API results calls this base method of the controller:
protected ActionResult GetApiResult(object instance) { if (string.IsNullOrEmpty(this.Format)) return null; if (this.Format == "json") return this.Json(instance); else if (this.Format == "xml") { string xmlResult = string.Empty; if (!SerializationUtils.SerializeObject(instance, out xmlResult)) throw new InvalidOperationException("Unable to serialize instance to Xml"); return this.Content(xmlResult, "text/xml", Encoding.UTF8); } else if (this.Format == "rss" || this.Format=="atom") { return GetFeed(instance); } return null; }
Format is automatically set off the query string var when the controller starts up. Really pretty simple to do.
The downside of this is that if an error occurs anywhere along this path the message won't be captured as the appropriate format but go out as HTML. Hence a seperate API that guarantees consistent output results.
# re: Introducing CodePaste.NET
thanks for sharing
# re: Introducing CodePaste.NET
http://codepaste.net/list/user/1133
http://codepaste.net/list/user/1133?format=rss
It'll also work user name but that's not hooked up yet. More to come along this way.
# re: Introducing CodePaste.NET
pamela
# re: Introducing CodePaste.NET
also individual language tab/link on top?
# re: Introducing CodePaste.NET
Pastebin.com offers syntax highlighting for lots of programming languages
# re: Introducing CodePaste.NET
Therefore, could you add an API that allows me to POST some code with fields for tags, comments, title etc; and get returned the short URL that can be added to a twitter post.
# re: Introducing CodePaste.NET
# re: Introducing CodePaste.NET
# re: Introducing CodePaste.NET
http://www.codinghorror.com/blog/archives/001257.html
I wonder how long it will be before we see something like this on stackoverflow...
# re: Introducing CodePaste.NET
0) be cool if there was a way to group pastes together, eg. sliverlight, mvc, mvc_talk_01 etc.
1) if there was a way to the snippet rendered into my blog via div and some little js include or via an iFrame. why keep this stuff in two places?
# re: Introducing CodePaste.NET
I put the site live at an early stage with just basic functionality, partially because that's really the core of what I need out of it, but also to see if there's even any interest in this sort of thing given that there are other sites that do something similar.
Keep the suggestions coming - I'll keep working on it or you can supply patches if you want to help out.
Biggest thing I think would be useful is to support more languages. Currently I'm using the Manoli component which works well for what's supported, but I think this base engine might be too limited for supporting very different language like PHP. I think I'll have to look around for a different tool. Suggestions?
# re: Introducing CodePaste.NET
Would be cool if there was some kind of embed functionality for embedding in a blog - not quite sure how that would work, but syntax highlighting in a simple blog is painful.
Cheers.
# re: Introducing CodePaste.NET
The way this would work would be with a script block:
<script src="http://codepaste.net/codescript/32i3dd" type="text/javascript"></script>
which then embeds the code and a link to the style sheet into the page.
I can create a live writer plug in for this, but if you're using a plug-in wouldn't one just use something like the Visual Studio code plug-in which is easier and more self contained to boot?
And if you don't have a plug-in of some sort would you actually use it and manually add an HTML tag into a blog post? I kind of doubt it?
I'll provide the feature (in a few days) but curious to hear some feedback of how people would use it. Or any other suggestions on how embedded might work differently for you.
# re: Introducing CodePaste.NET
Can you 'subscribe' to someone's snippets? That would be useful.
Great Job,
Doug