5

I am working on ASP.NET MVC web application and I want to allow all file extensions in URL of my application. I have tried adding

<add name="ChatFileHandler" 
     path="*.docx" 
     verb="GET"
     type="System.Web.Handlers.TransferRequestHandler" 
     preCondition="integratedMode,runtimeVersionv4.0" />

inside /<system.webServer>/<handlers> in Web.config but it will only allow .docx file extensions in url. I want my url to be /Download/{FileName}.extension.

How can I achieve my desired functionality with less workaround.

Regards.

Edit: I have also tried adding below route setting in AreaRegistration.

context.MapRoute( "FileDownload", "Download/{fileName}.{datatype}", new { controller = "Download", action = "Download", fileId = UrlParameter.Optional, fileName = UrlParameter.Optional } );

Controller:

public ActionResult Download(string fileId, string fileName, string datatype) { }

Alongwith <add ChatFileHandler ... /> with path="*.docx" in Web.config. Adding these i am able to get fileName and datatype in controller's action method. But I don't want to add handler for every file extension as they will be in hundreds.

Ammar
  • 177
  • 12

1 Answers1

4

You could disable static files handler for specific path (and HTTP verbs) by adding following handler in web.config:

<system.webServer>
<!-- -->
    <handlers>
        <add name="Download" path="/Download/*" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
</system.webServer>
CodeFuller
  • 30,317
  • 3
  • 63
  • 79