如何使用 ASP.NET 为文件夹设置特定的 404?

发布于 2024-08-04 08:17:58 字数 706 浏览 3 评论 0原文

这有点棘手,如果你们能给我一些关于这个问题的指示,我会很高兴。
这是我想要做的:

  • 用户尝试访问 myapp.com/data/123456.mp3

  • test.mp3 不存在

  • 系统将用户发送至 myapp.com/data/error.apsx?file=123456.mp3

我需要这个来处理大型系统应该提供 mp3 文件的方式。

如果用户尝试访问 myapp.com/otherFolder/notHere.whatever,系统通常会返回标准 404 错误。

我知道有多种方法可以在 IIS 中指定这一点,但如果我可以通过编程或仅在我的 .net 项目中执行某些操作,我会很高兴。

编辑:

我在 myapp.com/data/ 中创建了一个 web.config 文件,

<?xml version="1.0"?>
<configuration>
    <system.web>
      <customErrors mode="RemoteOnly" defaultRedirect="/data/mp3/full/serveMp3.aspx"/>
    </system.web>
</configuration>

这似乎不起作用。

This is a bit tricky and I'd be glad if you guys could give me some pointers on this one.
Here is what I want to do:

  • A user tries to access
    myapp.com/data/123456.mp3

  • test.mp3 doesn't exist

  • The system sends the user to
    myapp.com/data/error.apsx?file=123456.mp3

I need this in order to handle the way a large system is supposed to serve mp3 files.

If a user tries to access myapp.com/otherFolder/notHere.whatever, the system returns the standard 404 error normally.

I know there are ways to specify that in IIS, but I'd love it if there was something I could do programmatically or just withing my .net project.

edit:

I've created a web.config file in myapp.com/data/

<?xml version="1.0"?>
<configuration>
    <system.web>
      <customErrors mode="RemoteOnly" defaultRedirect="/data/mp3/full/serveMp3.aspx"/>
    </system.web>
</configuration>

This doesn't seem to be working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆头 2024-08-11 08:17:58

自定义错误
http://aspnetresources.com/articles/CustomErrorPages.aspx

在您的 web.config 中。

<customErrors
       mode="RemoteOnly" 
       defaultRedirect="~/errors/GeneralError.aspx" 
/>

Custom Errors
http://aspnetresources.com/articles/CustomErrorPages.aspx

In your web.config.

<customErrors
       mode="RemoteOnly" 
       defaultRedirect="~/errors/GeneralError.aspx" 
/>
思念绕指尖 2024-08-11 08:17:58

您要做的第一件事是确保 ASP.NET 能够处理这些文件请求,因为默认情况下 .mp3 不是 ASP.NET 扩展名,这将仅由 IIS 处理。

一旦你处理了这个问题,你的脑海中就会浮现出几种实际做到这一点的方法。

第一个是创建一个 HttpModule 来监视 OnUnhandledException 事件。由于 ASP.NET 将 404(以及所有 HTTP 错误)作为 HttpException 类型异常抛出,因此该模块将为您提供一个捕获、解析请求并重定向到您自己的目的的地方。

另一种方法是在您关心的文件夹级别创建一个 web.config(记住这些可以嵌套)并在那里创建 customerror 部分。这更直接,但提供的控制要少得多。综合考虑,我总体上更喜欢这个模块。

The first thing you have to do is make sure ASP.NET gets to handle these file requests since by default .mp3 isn't an ASP.NET extension and this will just be handled by IIS.

Couple of ways to actually do this once you are handling it spring to mind.

The first is to create an HttpModule which watches the OnUnhandledException event. Since ASP.NET throws 404's (and all HTTP errors) as HttpException type exceptions the module will provide you with a place to catch, parse the request, and redirect to your own ends.

The other means is to create a web.config at the folder level you care about (these can be nested remember) and create customerror section there. This is more straightforward but affords much less control. All things considered I would favour the module generally.

挖鼻大婶 2024-08-11 08:17:58

也许您可以将 web.config 放入要放置特定错误的文件夹中,然后在该 webconfig 中放入带有指示页面的 customerror 标记
或者您可以在主 web.config 中使用位置标签,

但我不确定这一点

probably you could put a web.config indide the folder that you want to put a specific error and put in that webconfig the customerror tag with the page indicated
or you can use the location tag inside the main web.config

but i'm not sure about this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文