Click here to Skip to main content
13,589,661 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 9 Jan 2012
Licenced CPOL

ASP.Net MVC (2 & 3) - Tracking number of downloads

, 9 Jan 2012
Rate this:
Please Sign up or sign in to vote.
This tip explains how downloads could be tracked for any file in your website or another website

At times, you would wish to see the number of downloads your app (or any file) gets. The following tip discusses how this could be achieved. The intention is, if the user enters the url (for ex.) http://localhost:12345/downloads/, track the download and then return the file that the user requested. This is how you do them in ASP.Net MVC 3 (should also work in MVC 2).

The first step would be to access the database layer to save the count. I am not discussing this as its out of scope for this tip. As we intend to serve a file instead of a view, we just need to return a FileResult that takes in 3 parameters (other overloads are also available). The parameters expected are:

  • The url as received from Url.Content [Url.Content expects a virtual directory]
  • The application type of the file to be served. In this case it's a zip file and so the applicationType is "application/x-compressed"

public ActionResult DownloadLink1()
    // access the database layer and increment the counter to
    // indicate that SampleApp was downloaded
    string url = Url.Content("~/Content/downloads/");
    return File(url, "application/x-compressed");

You may also wish to track the number of downloads you app (or some file) gets, when its hosted on another site. In that case, you cannot just use Url.Content as it expects a virtual path. So you will have to use the WebClient class to first download this file and then return it. In this case we use another overload of FileResult, as we are downloading the file and then presenting it to the user. This overload takes in a 3rd parameter which is the file name that the client sees while the download link is clicked (the client is free to change the name, of course). This overload of FileResult takes in a byte stream as the first parameter instead of a path returned by Url.Content. The first step just like the previous method would be to access the database layer to save the count.

public ActionResult DownloadLink2()
    // access the database layer and increment the counter to
    // indicate that SampleApp was downloaded

    byte[] file = null;
    using (var client = new WebClient())
        file = client.DownloadData("");
    return File(file, "application/x-compressed", "");

Now, I will give an example as to how these action methods could be used. Lets assume these 2 action methods are in a controller called DownloadsController. We need to generate routes so that they could be added to various views. If you observe, unlike normal routes which doesn't have an extension in the url (in mvc), these url's need to have an extension so that the url indicates that it is indeed pointing to the path of the file about to be downloaded. So instead of just using Html.ActionLink we need to use Html.RouteLink, which generates url's depending on the format given in Global.asax.cs file. To achieve this, add the 2 routes to the Global.asax.cs file's RegisterRoutes method (text in bold).

public static void RegisterRoutes(RouteCollection routes)

    routes.MapRoute("DownloadRt1", "downloads/",
        new { controller = "Downloads", action = "DownloadLink1" });

    routes.MapRoute("DownloadRt2", "downloads/",
        new { controller = "Downloads", action = "DownloadLink2" });

        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults

Finally, given below is a cshtml file that utilizes these 2 links

    ViewBag.Title = "Index";


@Html.RouteLink("download link 1", "DownloadRt1")
<br />
@Html.RouteLink("download link 2", "DownloadRt2")

The above Html.Routelink would generate the following links (assuming base url is http://localhost:12345) - http://localhost:12345/downloads/ and http://localhost:12345/downloads/ Html.RouteLink is a helper method that generates links using the route name (From Global.asax.cs passed in as the 2nd parameter. Clicking on these links would would call the database layer to log this download. Hope this tip was hepful!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Karthik. A
Software Developer (Senior)
United States United States
Just another passionate software developer!

Some of the contributions to the open source world - a blog engine written in MVC 4 - sBlog.Net. Check it out here. For the codeproject article regarding sBlog.Net click here!

(Figuring out this section!)

You may also be interested in...

Comments and Discussions

GeneralYou always have the option of improving upon a tip. Is this ... Pin
Karthik. A17-Jan-12 5:08
memberKarthik. A17-Jan-12 5:08 
GeneralReason for my vote of 1 This implementation only seems to ma... Pin
CUShane17-Jan-12 4:41
memberCUShane17-Jan-12 4:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.180615.1 | Last Updated 9 Jan 2012
Article Copyright 2012 by Karthik. A
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid