• Routing a hiearchical path in ASP.NET MVC

    If you wonder how to make a GitHub (and other websites) like routes to access to hierarchical paths or files, here's the way to do it in ASP.NET MVC. GitHub is a GIT hosting service, and allows you to browse the repositories. When doing so, it uses a path as a routing parameter, as seen in this URL: https://github.com/erikzaadi/GithubSharp/blob/master/Core/Models/Commit.cs. This includes slashes and so to represent the directories, and is a parameter that depends on the file location inside the repository. A route like this can be done in ASP.NET using the called catch-all parameter.

    The catch-all parameter allows you to use a wildcard on a route, so it takes everything after a given route as a single parameter. You can find the explanation of this feature in the ASP.NET Routing MASDN help page.

    All you need to do to make a route like the one in the example to work is add this code to your Global.asax file, in the RegisterRoutes method:

        new { controller = "Blob", action = "GetContents" }

    This will pass the controller Blob a parameter called path that will contain the parameter you want (in the example that would be master/Core/Models/Commit.cs. All you have to do now is use this parameter as you wish so you can access the desired file and show it's contents on a web page.

  • Get the number of days in a month in .NET

    If you've ever wondered how to get the number of days for a given month in .NET, the solution is pretty easy:

    System.DateTime.DaysInMonth(int year, int month)

    Hope it helps!

  • Execute javascript code every time a form is loaded via Ajax.BeginForm

    If you've ever used the Ajax.BeginForm code to render a form in ASP MVC using Ajax (via jQuery, for example), you may have wondered there's an object you can pass to the call called AjaxOptions. This object allows you to pass the BeginForm call some interesting options to further enhance the form when it's rendered. One of those options is specially useful if you want some javascript to be executed just after the form has been rendered. You can always use the $(document).ready in jQuery, but this code will not be called if, for example, the form is rendered after a postback and you have some validation errors (a missing required field, for example). Fortunately, you can use the AjaxOptions object to tell the form to execute a method right after the form has been loaded (for the first time or after a postback).

    Read on →

  • Creating and consuming JSON data in MVC 2

    Recently I started working in a project using MVC 2. I found out a very useful feature to retrieve JSON data from the server using an Action in a Controller.

    This feature is in the base Controller class, which we inherit when we create a new controller. If you take a look into the different methods this class has you'll find those ones:

    // Summary:
    //     Creates a System.Web.Mvc.JsonResult object that serializes the specified
    //     object to JavaScript Object Notation (JSON).
    // Parameters:
    //   data:
    //     The JavaScript object graph to serialize.
    // Returns:
    //     The JSON result object that serializes the specified object to JSON format.
    //     The result object that is prepared by this method is written to the response
    //     by the MVC framework when the object is executed.
    protected internal JsonResult Json(object data);

    Along with this method, there are some overloads that allow more parameters, you can see them all in here: Controller.Json Method. In this example I'll use the first one, which is the simplest.

    Read on →

  • C#, the "and" operators and lazy evaluation

    Today at work we found a bug. My workmate, not used to C#, usually uses the & operator to compare boolean values. However, in C#, the & operator does not use lazy evaluation.

    One curious thing about C# is that it can use two different operators to calculate an and expression: the & operator and the && operator. The difference between both is that the first one (&) can be used both with integer types and boolean types. When used with integer types it will perform a bitwise comparison between the two, and when used with boolean values it will use the logical and operation between the two boolean values, evaluating all the parts of the expression. This means that using a code like this one:

    if (someObject != null & someObject.SomeProperty == someValue)

    will throw a runtime error if someObject is null, because it will try to obtain the SomeProperty value.

    However, the && operator is only available to boolean expressions, and it uses lazy evaluation, this is, if the first condition evaluated is false, it will calculate false without evaluating the rest of the expression, because an and is only true if all the expressions are true.

    Conclusion, be sure to always use && when evaluating boolean values if you want to avoid run time surprises :).