Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow all extensions in ASP.NET MVC URL

Tags:

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.

like image 560
Ammar Avatar asked Mar 26 '18 13:03

Ammar


1 Answers

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>
like image 128
CodeFuller Avatar answered Sep 22 '22 12:09

CodeFuller