Search code examples
c#asp.net-mvcurl-routing

Allow all extensions in ASP.NET MVC URL


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.


Solution

  • 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>