Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

Introducing CodePaste.NET


:P
On this page:

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.

  1. You create a new Code Snippet by entering your code and giving it a title
  2. You ‘paste’ it
  3. You capture the link from the Address Bar
  4. You reference the link in a Social Network client
  5. Other people can click the link and view your pasted code
  6. Discussion ensues! (hopefully)

To create a new code snippet just takes a few seconds using a simple input form:

Edit

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:

 CodesnippetDisplay  

The link looks something like this:

http://codepaste.net/x5ndfj

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:

Tweet

Skype

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:

RecentView

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…

Posted in ASP.NET  MVC  

The Voices of Reason


 

Doug Osborne
July 21, 2009

# re: Introducing CodePaste.NET

Rick,

Can you 'subscribe' to someone's snippets? That would be useful.

Great Job,
Doug

Tom
July 21, 2009

# re: Introducing CodePaste.NET

Very nice Rick,

One thing I would like to see is a checkbox that prevents my snippets from appearing in 'Recent Snippets'...

KG
July 21, 2009

# re: Introducing CodePaste.NET

Very cool Rick,

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.

Phil Derksen
July 21, 2009

# re: Introducing CodePaste.NET

Awesome work Rick! Very useful tool, but also thanks for open sourcing it. This will be helpful to me as I'm in the middle of an ASP.NET MVC app myself.

Kevin Radcliffe
July 21, 2009

# re: Introducing CodePaste.NET

Great job! The API Returns are a must, really liking the look/feel too. Thanks for putting this out there.

Rick Strahl
July 21, 2009

# re: Introducing CodePaste.NET

@KG - the API returns are handled by really simple mechanism (in baseController) which is fairly generic.

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.

Eber Irigoyen
July 21, 2009

# re: Introducing CodePaste.NET

as always, great work

thanks for sharing

Rick Strahl
July 21, 2009

# re: Introducing CodePaste.NET

@Doug - actually user name lists work already, although it's not implemented through the UI.

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.

PThalacker
July 21, 2009

# re: Introducing CodePaste.NET

I am always disheartened by how far behind the curve I find myself on new technologies. In my Google bookmarks I have a blog from Scott Guthrie written Nov 2007 and a Phil Haack blog from June 2008. The fact that you just did your first MVC app gives me such hope. I have only just implemented Linq in the last two months. Your TimeTrakker app has been invaluable in accelerating my comprehension. Your three blogs on dealing with disconnected Linq entities saved me. Thank you so much for all your contributions. You have made my life easier in too many ways to tell.

pamela

zucvv
July 21, 2009

# re: Introducing CodePaste.NET

Add the "Saved Snippets" or "Favourite Snippets" section pleeeeeeese. It make your frequent used snippet codes quickly accessible specially when your code collection grows.

also individual language tab/link on top?

pastebin.com
July 21, 2009

# re: Introducing CodePaste.NET

How is it different from pastebin.com?

Pastebin.com offers syntax highlighting for lots of programming languages

PiotrB
July 22, 2009

# re: Introducing CodePaste.NET

Very nice. What about small VS.NET add-in

Dan Petitt
July 22, 2009

# re: Introducing CodePaste.NET

I would love to add support for this into my twitter client, "twitcher".

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.

Travis Laborde
July 22, 2009

# re: Introducing CodePaste.NET

Very nice - thanks! Maybe a nice Visual Studio Add-In, where you select, right click, and "CodePaste Selection" would be cool too :)

nando
July 22, 2009

# re: Introducing CodePaste.NET

Nice! And most important, it works well, thank you! :-)

mike johnson
July 22, 2009

# re: Introducing CodePaste.NET

this is great. I remember when atwood talked about doing this in his article :

http://www.codinghorror.com/blog/archives/001257.html

I wonder how long it will be before we see something like this on stackoverflow...

mike johnson
July 22, 2009

# re: Introducing CodePaste.NET

After using it, i thought:

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?

Rick Strahl
July 23, 2009

# re: Introducing CodePaste.NET

Thanks for all the suggestions. Some of these are already on my list to add - searching obviously will be necessary, user listing and linking, favorites and some sort of mechanism to privatize and show snippets only to a specified group that the user allows. Additional syntax support... yup lots of things to add :-}

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?

DotNetShoutout
July 23, 2009

# Introducing CodePaste.NET - Rick Strahl

Thank you for submitting this cool story - Trackback from DotNetShoutout

David Wynne
July 27, 2009

# re: Introducing CodePaste.NET

Looks good - I like pastebin so anything that improves or adds to that set of tools is good.

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.

Rick Strahl
July 27, 2009

# re: Introducing CodePaste.NET

Several people have asked for embedding and I have code working for this. But I really wonder if this would be used because you have to somehow embed this into a blog as code somehow.

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.

Lee C.S. Young
August 05, 2009

# re: Introducing CodePaste.NET

I know this is nitpicking, but a link back to the main page from the logo is something that I am used to seeing on most websites, and it's missing on codepaste.net. :)

PimpThisBlog.com
August 11, 2009

# Introducing CodePaste.NET - Rick Strahl's Web Log

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

Jamie Nordmeyer
August 17, 2009

# re: Introducing CodePaste.NET

Very nice, Rick! I should put some stuff on here. A great idea for posting small snippets for others to use, without writing out a full blown article like you would for CodeProject or CodeGuru. Thanks for providing this!

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024