One of the newest additions to the .NET 4 framework is MEF. MEF stands for the Managed Extensibility Framework and is the answer to your prayers if you are building an app that allows developers to extend its functionality by building plugins. MEF makes the task of loading assemblies and utilizing their functionality very simple. Before we go any further there are a few basic terms that you must understand before you start using MEF: The Catalog is responsible for loading assemblies. There are several types of catalogs to chose from such as DirectoryCatalog and AssemblyCatalog which we will discuss in more detail later on. The Container holds one or more catalogs. The Export attribute is used to decorate objects so they can be consumed by MEF. The Import attribute lets MEF know that an object is the target for one of more exported objects. A part is any object that is exported or imported. So in order to start using MEF, you need to create one or more ca...
[More]
If you have been developing applications in ASP.NET MVC then you are probably familiar with the ActionResult class. The ActionResult is the most common type of object returned from an action. When building MVC apps, most of time you will use the ActionResult class. Last week while I was working on my open source project WeBlog, I built an HTTP Handler to serve up images. I started using an HTTP Handler for images because I needed a mechanism to prevent bandwidth leeching. The only bad thing about using an HTTP handler for images is that you end up with some pretty ugly URLS. In my case the URL ended up looking like this: /Image.axd?image=sample.png Luckily, my friend Ron noticed my new HTTP Handler and mentioned that I could have accomplished the same thing with a controller action that returned a FileResult instead. After a bit of investigation, I realized that Ron was absolutely right. I deleted my HTTP Handler and replaced it with this code, which was added to the Home Controlle...
[More]
While working on the WeBlog project I realized that I needed a star rating system for blog posts. A star rating allows your readings to rate content based on a 0-5 scale.
A fully lit star represents a full point on the rating scale. Therefore in order to give half point increments each star uses two images.
Left off: Left on: Right off: Right on:
When you put a left and a right image together it forms a complete star. So If you have a rating a 3.5 you would have the following stars displayed: Star #1 left on, right on #2 left on, right on #3 left on, right on #4 left on, right off #5 left off, right off.
Displaying the Current Rating
In MVC, the logic to determine which stars (images) should be initially displayed is best accomplished with a HTML helper:
public static string Ratings(this HtmlHelper helper, PostModel post) {
StringBuilder sb = new StringBuilder();
sb.AppendFormat("<span class='rating' rating='{0}' post='{1}' title='Click to cast vote...
[More]
Validating user input on the client definitely has its advantages. It not only avoids unnecessary round trips to the server but it can also drastically improve the user experience. When I think about client side validation I think about jQuery. If you are familiar with jQuery, then you know that there are a ton a plug-ins available. Just like an Apple IPod has “an app for that”, jQuery has a “plugin for that”. Anyway, after a quick search I discovered the jQuery Validation plugin. There are two different ways to use the Validation plugin. The first way, is to the pure JavaScript route. This means you establish all the validation rules, messages and callback events from code. The second way is to decorate your input fields with special CSS classes. The plugin will then inspect the class attribute of each field at runtime and apply validation rules accordingly. In the upcoming example, I used the CSS approach. However, if you decided that you want to go in the other direction ther...
[More]
I recently announced the WeBlog project. WeBlog is a blogging platform which will support multiple data providers. Out of the box I plan on offering SQL Server and XML support. Most people like the XML option because it drastically reduces web hosting costs. The only problem with XML is that it can be painful to work with. In general, XML makes me want to pull my hair out! When building a blog you have a few basic entities that you need to deal with. Most typical blogs have posts, categories, tags, users and roles. Therefore I made an XML file to represent each of these items. However, for this tutorial I will focus on parsing the XML for categories. For a point of reference here is the XML structure that I am using to store category information: <?xml version="1.0" encoding="utf-8"?>
<categories>
<category id="19770e74-9ec9-4cde-b2ab-e5051aaaf348" description="Posts about my adventures with WeBlog"
par...
[More]