Share via

Is AddDirectoryBrowser is optional.

Amit Singh Rawat 731 Reputation points
2021-07-27T14:50:07.143+00:00

Microsoft document says, to enable directory browsing we need to add services.AddDirectoryBrowser().
I tried same example in Microsoft site and able to browse directories without adding AddDirectoryBrowser.
Is it optional. If yes, why it is not mentioned in the Microsoft Site. Did I miss something?

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-5.0

Visual studio 2019. Version 16.9.3.
.NET5

Developer technologies | ASP.NET Core | Other

Answer accepted by question author

Rick0 76 Reputation points
2021-12-08T03:32:31.917+00:00

You might be able to get away with not calling AddDirectoryBrowser in some cases, but it should be called. I updated the doc to explain why. See the paragraph just above [Serve default documents][1] which has links to the relevant source code. In the sample code I wrote for the doc you can remove the call to AddDirectoryBrowser and all the sample code works. I think that's why @Amit Vasu Rawat was asking.

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Rick0 76 Reputation points
    2021-12-08T03:39:27.577+00:00

    @Bruce (SqlWork.com) how do I change my profile? I might work for MS, but I'm never on this site.

    Edit I can see my profile. How did you remove your email from your profile?

    Was this answer helpful?

    0 comments No comments

  2. Bruce (SqlWork.com) 84,061 Reputation points
    2021-07-27T15:22:04.417+00:00

    IIS supports directory browsing, but its for the site. You will expose the .net core code.

    The .net core static file handler (used instead of IIS for static files), also supports directory browsing. This only allows browsing of the wwwroot folder, not the site. it also works when not hosted by IIS.

    Was this answer helpful?


  3. Michael Taylor 61,221 Reputation points
    2021-07-27T15:06:12.08+00:00

    Directory browsing is disabled by default as it is a security risk. I question why you would turn it on in an MVC application other than for a specific folder (and even then it seems odd to me). You have to add the calls mentioned in the link.

    Please provide a link to the example from MS that you see where they don't have this code and browsing is working (note that if you configure it in IIS then you're skipping MVC which would be bad).

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.