Daily Archives: May 11, 2009

ASP.NET MVC – Helpers

This is the third in a series of posts about things I found out while learning ASP.NET MVC. Today : Helpers.

Helpers are, as their name suggests, helpful things. If, for example, you want to add the current assembly version information into a page, you are likely to run into a problem or two. ASP.NET MVC doesn’t create code-behind pages for you to do this kind of useful operation in. Instead, you have to fall back to the old Classic ASP technique of coding directly into the view page. That is far from ideal. For one thing, the following code snippet doesn’t produce the correct reult directly from a page.

<%= System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); %>

My experience of that was that it would, if you were very luck, print “0.0.0.0” rather than the current version.

So, how did I achieve it? I created a helper. Basically a helper is a static class, containing static methods (including extension methods)

Here’s an example of my SiteWideHelper.cs file

using stuff;

namespace Helpers
{
    public class SiteWideHelpers
    {
        public static string SiteVersion()
        {
            return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }

        public static string CurrentController(this HtmlHelper helper)
        {
            return (string)helper.ViewContext.RouteData.Values["controller"];
        }
    }
}

The helper class contains one “vanilla” method and one extension method to the HtmlHelper class. The first method, SiteVersion, simply treturns a string of the current assembly version. The second method, CurrentController, returns the name of the current controller by querying the extended HtmlHelper class.

Both of these methods may now be called, and their output rendered directly to the page. (Actually, I use the Currentcontroller method to determine which bits of sub-navigation are displayed on the master page.

<span>Web site version: <%= Html.Encode(SiteWideHelpers.SiteVersion()) %></span>

Don’t forget, instead of haveing to reference the fully qualified name of your method / class, you can import the namespace into your view

<%@ Import Namespace="Helpers" %>

I’m sure I’m missing some concepts here that would make development so much simpler, but for astarting point, this is working for me

ASP.NET MVC – Deploying on Win XP

This is the second in a series of posts about things I found out while learning ASP.NET MVC. Today : Setting up your development XP machine to run your site.

This is actually quite a simple one, but be warned, the overhead of setting up IIS5 to run an MVC site may lead to trouble if you put it under heavy load.

First, you’ll have to add a new Route to your Global.asax.cs file, just for IIS5.  For example :

 

routes.MapRoute(
      "IIS5",  // Route name
      "{controller}.mvc/{action}/{id}",  // URL with parameters</strong>
      new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

Note in this example that the only difference (apart from the name) is the addition of a “.mvc” extension after the controller declaration.

Now, you just need to configure your IIS virtual directory.

In inetmgr, navigate to your virtual directory, right click it and select Properties.  On the default tab (“Directory”), click the Configuration button.  You now need to add a new file extension handler, so click on the “Add” button.

In the textbox for the executable, enter the path to aspnet_isapi.dll ([probably C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll), and set the extension to be “.*”.  Before you hit “OK”, make sure you untick the box marked “Check that file exists”.  Now you can hit OK a few times and you’re good to go.

ASP.NET MVC – Preparing for the real world

This is the first in a series of posts about things I found out while learning ASP.NET MVC. Today : adding jQuery support that will work wherever the app is deployed in a directory structure.

In order to reference the jQuery javascript files, I had to figure out how to add a realtive link to the .js files that would work even if the app was deployed to an unknown virtual directory. A quick question on StackOverflow.com led me to the following steps.

1) Create an extension method in the Helpers namespace…

public static string GetBasePath(this HtmlHelper helper)

{

    var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);

    return urlHelper.Content("~/");

}

Next, point the link tags for my js file to the new location…

<script src="<%= Html.GetBasePath() %>Scripts/jquery-1.3.2.js" type="text/javascript"></script>
 

That’s it.  All I had to do.

Of course, since I want to call data back from my MVC site via jQuery, I had to take a couple of extra steps to make the base path available to my scripts.  Back in my master page, I created the following snippet.

<script language="javascript">

  var rootUrl = "<%= Html.GetBasePath() %>"; 

</script>

Now I can call the data back from the site within my custom-defined jQuery script as follows…

$('#preview').click(function() {

    $.get(rootUrl + "jquery/getprovider/" + $(this).attr("name"), function(data) { $('#detailDiv').slideDown(300); $('#detailDiv').html(data); });

    });

My #preview object has an id coded into its ‘name’ attribute so the jQuery controller can reference the correct data., eg.

<a href="#" name="12345">details</a>