We use an application at work which is supplied as a single exe file which is downloaded from their website. The issue is the file has always been called application.exe irrespective of the version. With each new version it just gets overridden and keeps the same file name.
We added a request to the manufacturer a request that the file name include the version so we a) know which version we are getting and b) can keep a couple of historical versions locally just in case. However; due to the length of time they have gone for the single filename it is now linked from many different places and they don’t want to break the links.
It got me thinking, this would be ideal for a route in Asp.Net MVC but can you set the route to accept a string “application.exe” and it do as required. The short answer is yes! :-)
The thinking was setup the route for the single filename. This would execute an action on a controller which would go off to a repository, find the latest version file and then return the file as a DownloadResult to throw up the save file dialog. I had a hunt round the internet and found an oldish post by Phil Haack for this kind of thing. I updated it to map to a specific file, for instance on a file share.
public class DownloadResult : ActionResult
{
public string FileLocation { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
public override void ExecuteResult(ControllerContext context)
{
if (!String.IsNullOrEmpty(FileLocation))
{
context.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + FileName);
context.HttpContext.Response.ContentType = ContentType;
}
context.HttpContext.Response.TransmitFile(FileLocation);
}
}
Once you’ve got that sorted all that’s left is to add in the route in your Global.asax …
routes.MapRoute("download", "application.exe",
new {controller = "Home", action = "Download"});
And tad ta :-)
The other options would be routing / redirecting at the IIS/Apache level, but as a developer this seemed to be the easiest and simplest way to enable data access to help with new version releases.
No comments:
Post a Comment