I would like to display a 404 not found error on directory browse instead of the standard 403 unauthorized message from asp.net but not for the files within. For example:
www.somedomain.com/services - 404 not found
www.somedomain.com/services/test.asmx - 200 ok
I tried to do a custom generic handler like so:
public class DirectoryBrowsingAttempt : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.StatusCode = 404;
}
public bool IsReusable
{
get
{
return true;
}
}
}
And in the under web.config:
<handlers>
<add name="NoAccess" verb="*" path="Services" preCondition="integratedMode" type="Namespace.DirectoryBrowsingAttempt"/>
</handlers>
This will throw a 404 for the directory and the files within. Is there a way to avoid the 404 for the files?
You just need to add a slash after the path. The only thing that I don't like about this though is sub-folders. You would have to add a specific config to each level of the directories.
Leaving it open, or adding an asterik as the end, will ignore all files in that path.
<handlers>
<add name="NoAccess" verb="*" path="Services/" preCondition="integratedMode" type="Namespace.DirectoryBrowsingAttempt"/>
</handlers>