Custom file extensions

Discussion in 'Microsoft Development' started by SubSpace, Dec 10, 2007.

  1. SubSpace

    SubSpace Bass

    Handle custom file extensions in ASP.NET

    Is there a way to make ASP.NET handle file extensions other than the ones configured by default that I'm overlooking in H-Sphere?

    Both ColdFusion and PHP appear to have the option to add additional extensions, but not ASP.NET? For example if I wanted to protect a .doc file on a server with .NET authentication, I'd normally add that extension and set a static file handler for it in the Web.config.

    In this case I want to rewrite an URL that ends with an extension other than .aspx. It looks like I can't use the .NET URL rewriting feature for that under H-Sphere though?
  2. Stephen

    Stephen US Operations Staff Member

    Re: Handle custom file extensions in ASP.NET

    you are correct that it is not in asp.net and we will do it but not for html/htm page titles(we did it for one and was badly abused, I know it wouldn't for all....but using it to parse all is hard on the server)
  3. tetranz

    tetranz Perch

    Subspace, one way I've achieved the same sort of thing is to deliver the file through ASP.NET. That doesn't require anything special on the host. I haven't tried this on JodoHost but it should work. If you can get it to work then it makes your code more portable.

    The key thing is the TransmitFile method of the Reponse object.

    Put the doc file somewhere where it is not directly accessible with a browser but still accessible from your code. Preferably outside the web root but I'm not sure if that works with medium trust so it may have to be in a password protected directory.

    Use Reponse.TransmitFile to read it and deliver it to the browser.

    Two tricks to do here: You really need to set a few http headers. I think you can get by with something like:

    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "application/msword";
    Response.AppendHeader("Content-Length", [length of file]);
    Response.AppendHeader("Connection", "close");
    Response.TransmitFile([Path to file]);
    Response.End();

    and ... browsers behave better if they see the correct extension which relates to what you're asking Stephen about but you can also do this sort of thing:

    http://www.example.com/download.aspx/myfile.doc

    That will run download.aspx but the browser thinks its getting a doc. You get the file name in code by parsing Request.PathInfo.

    Cheers
    Ross
  4. SubSpace

    SubSpace Bass

    I'm aware of transmitting files through a script to protect them, but in this case it wasn't about protecting downloads :)

    Thanks for the suggestion on the / notation though, I hadn't thought of that one, even though I use the syntax daily for webservice calls.. duh :rolleyes:

Share This Page

JodoHost - 26,000 hosting end-users in 100 countries
Plesk Web Hosting
VPS Hosting
H-Sphere Web Hosting
Other Services