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.

5
  • Do you not understand what this , path="*.docx", means? Commented Mar 26, 2018 at 13:33
  • This means any file with extension ".docx" is allowed in url. I want to allow all file extensions in url. Commented Mar 26, 2018 at 13:36
  • 2
    So, try path=".*" Commented Mar 26, 2018 at 13:37
  • Possible duplicate Commented Mar 26, 2018 at 13:38
  • @RyanWilson path=".*" didn't work. @Lennart not a duplicate. Because the answer of question means i have to ignore all file extensions like routes.IgnoreRoute("{file}.js"); Commented Mar 26, 2018 at 13:49

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.