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]
One of the best ways to keep your skills sharp as a developer is to start your own open source project. For the last 6 months or so I have been trying to come up with ideas on what kind of application I want to build. Since I enjoy blogging so much I decided that I will build my own blogging platform. There are a lot of good blogging platforms already available such as Wordpress, dasBlog and BlogEngine.NET. In order to make something comparative to these products will take a fair amount of time and effort. However, having a pet project like this is fun and I am really looking forward to working on it. The WeBlog (pronounced We Blog) project is hosted on codeplex. I have already checked in some code and things are starting to take shape even though it is still early in the process. If you want to compile the code you will need need Visual Studio 2010. I figured there is no point starting a project on old technology so I went for the latest and greatest technology with MVC 2 and the .N...
[More]
I have been working on a new project where I wanted to introduce the concepts of themes. This means that end users will be allowed to change the look and feel of the web application from a configuration menu. In order to deliver this functionality I needed to be able to dynamically change the master page on the fly.
My initial response was to use the Page PreInit event. The Page PreInit event is called write before the master page is assigned and therefore makes it a good candidate for dynamically assign the the master page. Unfortunately this is a Page level event. In order to use this solution I would need to override this method on every page. This would be a maintenance nightmare so I immediately eliminated this as a viable solution.
Another solution is to use the View method to assign the master page.
public ActionResult Index()
{
return View("Index", MyApp.Properties.Settings.Default.Theme);
}
Unfortunately, this solution is also a maintenance nightmare bec...
[More]
I was watching a video named "Mastering jQuery" on Tekpub a few days ago and I heard the term "Unobtrusive JavaScript". Basically unobtrusive JavaScript is a technique that emphasizes the separation of your functional layer from your presentation layer. In layman's terms this means that your JavaScript is NOT placed directly in your HTML. Instead it is placed in a separate external file. Hopefully, you are already doing this with your cascading style sheets. Why? So you can change the look and feel of your website without having to modify each page. The same principal applies to Unobtrusive JavaScript. Here is an example... In the old days it was very common to add a control to your HTML and wire up the event directly on the controls. Although this works fine it does not give you flexibility in your design. If you have similar controls on other pages that call the same function you will have to modify each page if you change the method signature or rename the func...
[More]