How to handle download file counter

Topics: Customizing Orchard, Writing modules
Jan 24, 2012 at 8:15 PM


I have installed the ContentViewCounter and I like to use to count hyperlink click count. The module seem not suited to this task because it count only when user show page content and not when an user click on a link to download a file.

My purpose is to count the number of downloads of an item pointed by an anchor html tag:

<a href="http://...." />

in ASP.NET <a> do a post back to ASP.NET code where I can handle the counter. I am new to MVC and Orchard so I cannot figure out how to handle clicks on <a> link and trigger some code behind.

Please any help is appreciated.


Jan 24, 2012 at 11:41 PM

You can go through a controller action.

Jan 25, 2012 at 7:54 PM


Thank you very much for your advice and for fast response. Although something say me that this is the right way to solve this task, I am trying to figure out how to turn your tips into code.  Sorry I am too lame in MVC.

If I have understood I can put ino href the url of my Controller action. Then when someone click on the <a> link the action will be triggered. There I will do something to register the click, perhaps increment a counter into the DB. Then I will redirect the user to the real URL !?! (but I miss somewhere where to store the final URL).

Am I correct or there is something else?

Thank you in advance for any additional advice.

Jan 25, 2012 at 8:23 PM

That's basically it. Your link might look like:


So then in your Download controller have an Index action like this:

public ActionResult Index(string filename) {

  // ... Do stuff

  return Redirect(mediaPath + filename);


Jan 27, 2012 at 10:06 PM
Edited Jan 27, 2012 at 10:08 PM


I have created a Orchard Module with a custom field to take care about download link data + a controller that update the database and redirect to the real download file link. Now the route to get to my controller does not work. 

The routing code seem pretty standard stuff:

namespace babelforNET.DownloadField
    public class Routes : IRouteProvider
        public void GetRoutes(ICollection<RouteDescriptor> routes)
            foreach (var descriptor in GetRoutes())

        public IEnumerable<RouteDescriptor> GetRoutes()
            return new[] {
                new RouteDescriptor {
                    Priority = 5,
                    Route = new Route(
                        new RouteValueDictionary {
                            {"area", "babelforNET.DownloadField"},
                            {"controller", "Download"},
                            {"action", "Index"}
                        new RouteValueDictionary(),
                        new RouteValueDictionary {
                            {"area", "babelforNET.DownloadField"}
                        new MvcRouteHandler())

And the controller is:

namespace babelforNET.DownloadField.Controllers
    class DownloadController : Controller, IUpdateModel 
        private readonly ISiteService _siteService;

        public DownloadController(
            ISiteService siteService,
            IShapeFactory shapeFactory)
            _siteService = siteService;

            T = NullLocalizer.Instance;
            Shape = shapeFactory;

        dynamic Shape { get; set; }
        public IOrchardServices Services { get; set; }
        public Localizer T { get; set; }

        // [HttpPost, ValidateInput(false)]
        public ActionResult Index(string filename)
            if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to list rules")))
                return new HttpUnauthorizedResult();

            return Redirect(filename);

        bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties)
            return TryUpdateModel(model, prefix, includeProperties, excludeProperties);

        public void AddModelError(string key, LocalizedString errorMessage)
            ModelState.AddModelError(key, errorMessage.ToString());

But when I enter the url: Downloads/File 
I get only 404

Please any help is appreciated.
Jan 28, 2012 at 10:25 AM

Your controller needs to be a public class.

Jan 28, 2012 at 12:10 PM

Thank you very much it worked :).