ASP.NET MVC routing: how to use matching file routing for a single folder?

I have a file on disk in the path /Files/filename.txt, and the MVC routing system checks if the file exists before using its routing logic (with controller, action ...).

I want to use routing logic before matching files on disk.

If I set "RouteExistingFiles = true", it will ALWAYS use routing for the corresponding files. I want this behavior to target only a specific folder.

"FileController" must be called even if the file in the Files folder exists. Basically, I want the opposite behavior of "IgnoreRoute ()".

How can i achieve this?

+4
source share
1 answer

You will need to take two approaches to this.

<system.webServer> <validation validateIntegratedModeConfiguration="false"/> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> <location path="files"> <system.web> <authorization> <deny users="*"/> </authorization> </system.web> </location> 
  • Make sure your runAllManagedModulesForAllRequests in your web.config is true so that UrlAuthorizationModule runs for static files.
  • You will need to specifically stop asp.net from serving the files in the file name folder. You can do this by adding a location entry to your web.config inside the node configuration. This specifically tells the authorization module that no matter who the user is, they do not have access to this folder.

After that, your FilesController will now serve files for the file folder.

0
source

Source: https://habr.com/ru/post/1340949/


All Articles