My Resume | Contact Me | RSS Feed | Follow Me on Twitter

Code Capers

The Ninja Coding Dojo
RSS Feed Twitter Email

Goodbye Http Handler, Hello FileResult

clock March 7, 2010 by author Michael Ceranski
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]


Building a Star Rating System with ASP.NET MVC and jQuery

clock March 1, 2010 by author Michael Ceranski
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]


Dynamic Master Pages in MVC

clock February 17, 2010 by author Michael Ceranski
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]


Making Your JavaScript Unobtrusive

clock February 3, 2010 by author Michael Ceranski
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]


Dynamic Select Lists with MVC and jQuery

clock January 27, 2010 by author Michael Ceranski
Update 1/28/2010: This article is Scott Guthrie approved (Corporate Vice President in the Microsoft Developer Division). There are occasions when you are developing a form and you need to have a drop down list that dynamically populates based on the value of another control. The classic example is a contact form that dynamically populates the states and provinces list when a country is selected. For example, when I select "United States" I want the child drop down to have entries like New York, Alabama, Texas and etcetera. When I select "Canada" I would expect to see provinces like Nova Scotia and Ontario. Fortunately, this functionality is very easy to build using ASP.NET MVC and jQuery. Best of all, we can do this without any postbacks using the jQuery Ajax functions. To get started make sure you have a reference to the jQuery library, preferably in the HEAD section of your html. For the purposes of this example I am using jQuery version 1.4. You can either ... [More]


About the author

MikeMy name is Michael Ceranski. I am a software developer from Buffalo NY. I have been writing code for over 10 years starting with Borland Delphi and later migrating to the .NET stack. I enjoy blogging about .NET, MVC and jQuery and I hope to spread my enthusiasm for technology by sharing my thoughts and ideas with you.

View my resume

Cumulus

This will be shown to users with no Flash or Javascript.

Sign in