I have a WebForms application running on IIS Express.
I’m using directoryBrowse=enabled
to let users access parts of the file structure in order to download and open files.
When I access a public directory on the website, a standard IIS directory listing page is displayed with the directory contents as usual.
I would like to register a custom handler specifically for directory browsing to control what is displayed and how when browsing directories. I know how to register a handler for a specific file or file type, however I don’t know how to register a handler for a directory (ideally the same handler for any directory globally).
2
Answers
I have found an answer myself. Posting for any finders with similar questions.
It turns out that I can register a handler for path
*/
:The server will then use this handler for any request ending with '/', except existing pages through FriendlyUrls or valid configured routes, which means it will use this handler for any directories in the server file tree. Then I create a new IHttpHandler called DirectoryHandler:
This will redirect any requests pointing to a directory to mypage.com/Browse/[Request.Path] Then, I register a new route in
RouteConfig.cs
:Implement
GenericRouteHandler
:Finally, I create the Browse page itself, where I can access the requested directory path via:
The rest is then just a matter of manually creating the views, permissions and behavior in the Browse page.
Hum, would it not be better to turn off folder browsing, and then say offer your own UI that is web based?
I note the above, since this VAST improves security, allows un-limited customizing of the UI. And say would allow limiting of folders and files based on the user’s logon.
And better yet, you can also use folders OTHER then the server, but not have to create and map virtual folders from the web server to those other servers.
This means, that for additional folders etc., you don’t have change the web server configuration.
Say you drop in a tree view control to the web page.
Chose the image set as "windows xp file explore".
So, now we have this markup:
So, code behind to "browse" folders is not a lot of code, but you can of course introduce restrictions on file types, and base the folder (limits) on the user’s logon.
So, say this code:
(it is not even recursive, and recursion is not required).
So, now the web page shows:
So, it is not clear if you need/want a custom handler, but only that of building some kind of UI with say a tree view control, and building your own file browse system as per above. Since I only load each folder “on demand”, then the speed of above is very fast, and even for a system with a large and deep folder hierarchy.
The above has check boxes for multiple file selecting, but you could have a click on a file download.
And the above design allows you to add additional UI options such as "select all this folder" or whatever.