一個站點根目錄下面有一個Config文件夾,這個文件夾里面都是一些json格式的txt文本,文本是一種靜態資源,如果知道這個文本的地址,就可以在瀏覽器中輸入地址打開這個文本,別人就可以看到站點的配置,這是不希望的結果,所以就需要讓這個文件夾禁止被瀏覽器訪問。
方法一:
把*.txt的文件后綴修改為*.config,asp.net默認不能夠訪問.cs、.config等后綴的文件。注:在vs中添加一個config文件,然后編寫需要的配置內容,推薦這種形式。
方法二:
<handlers>
<add name="test" path="/config/*.txt" verb="*" type="System.Web.HttpForbiddenHandler"/>
</handlers>
HttpForbiddenHandler:

1 using System; 2 3 namespace System.Web 4 { 5 internal class HttpForbiddenHandler : IHttpHandler 6 { 7 public bool IsReusable 8 { 9 get 10 { 11 return true; 12 } 13 } 14 15 internal HttpForbiddenHandler() 16 { 17 } 18 19 public void ProcessRequest(HttpContext context) 20 { 21 PerfCounters.IncrementCounter(AppPerfCounter.REQUESTS_NOT_FOUND); 22 throw new HttpException(403, SR.GetString("Path_forbidden", new object[] 23 { 24 context.Request.Path 25 })); 26 } 27 } 28 }