仅通过代码从特定目录提供静态文件
我正在寻找一种基于代码的解决方案,仅使用 ASP.NET 路由从指定目录提供静态文件,而不是在 Web.config
中指定 HttpHandler
。
例如:
我有以下部分目录布局
/
/public
/public/hello.txt
/public/css
/public/css/base.css
/readme.txt
如果从 /public
目录请求任何文件,则应按原样提供它们。如果请求任何其他静态文件(例如 /readme.txt
),则应将其作为 404
提供。允许提供静态文件的目录应该可以轻松地指定为字符串(var publicDir = "public";
)。
我希望在 Web.config
文件中几乎不需要任何配置,并且大部分配置都是在代码中完成的。
I am looking for a code based solution to only serving static files from a specified directory using ASP.NET routing rather than specify a HttpHandler
in the Web.config
.
For example:
I have the following partial directory layout
/
/public
/public/hello.txt
/public/css
/public/css/base.css
/readme.txt
If any files are requested from the /public
directory they should be served as-is. If any other static files are requested for example /readme.txt
it should be served as a 404
. The directory which allows static files to be served from should be easily specified as a string (var publicDir = "public";
).
I am looking to do this with little to no configuration required in the Web.config
file, and most of it configured in code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为不可能使用类库来实现解决方案。在 IIS 6 环境中,扩展名为 .TXT 或 .JPG 的文件无需通过 .NET 框架即可提供。唯一的方法是更改 IIS 配置。
在IIS 7中,事情的处理方式有所不同,但我不知道是否可以找到适用于所有环境的解决方案。
我很高兴听到其他人对此的看法,并在我感到疲倦时纠正我。
I don't think it would be possible to achieve a solution using a class library. In IIS 6 environment files with .TXT or .JPG extension are served without going through .NET Framework. The only way is changing IIS configuration.
In IIS 7 things are handled differently but I have no idea if it is possible to find a solution that works in all environments or not.
I'll be pleased to hear what others say about this and correct me if i'm worng.