MVC 网站 - 如何防止访问静态文件 [英] MVC website - how to prevent access to static files

查看:56
本文介绍了MVC 网站 - 如何防止访问静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个没有身份验证的 MVC 网站.在网站目录中,我有一个文件夹 \ProjectNotes,其中包含一个文件 Notes.txt.此文件夹和文本文件都不是解决方案的一部分,但仍会在发布时复制.

I have a MVC website, without authentication. In the website directory I have a folder \ProjectNotes which contains a file inside, Notes.txt. Neither this folder nor the text file are a part of the solution, but still get copied up on publish.

我的问题是,任何人都可以使用网络浏览器访问 mysite.com/ProjectNotes/Notes.txt 而我无法阻止它.我已尝试添加特定路由以将用户带到错误页面(无效),并且在我尝试过的 Web 配置中,但这也不起作用.

My problem is that using a web browser anybody can access mysite.com/ProjectNotes/Notes.txt and I cannot prevent it. I have tried adding a specific route to take the user to the error page (no effect) and in the web config I've tried but that didn't work either.

如何使用 MVC 框架阻止访问 Notes.txt 文件?

How can I prevent access to the Notes.txt file using the MVC framework?

推荐答案

您应该为您的案例使用 Web.Config 文件.将您的根 Web.Config 文件添加到 部分:

You should use Web.Config file for your case. Add to your root Web.Config file into <system.webServer> section:

<security>
  <requestFiltering>
    <hiddenSegments>
      <add segment="ProjectNotes"/>
    </hiddenSegments>
  </requestFiltering>
</security>

实际上,您的问题与 MVC 无关,但有 IIS 限制(或其他 Web 服务器限制,但我假设您使用的是 IIS)

Actually, your question has nothing with MVC, but with IIS restrictions (Or other web server restrictions but I assume you using IIS)

这篇关于MVC 网站 - 如何防止访问静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆