简体   繁体   English

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

[英]How to have a specific 404 for a folder using ASP.NET?

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 用户尝试访问myapp.com/data/123456.mp3

  • test.mp3 doesn't exist test.mp3不存在

  • The system sends the user to myapp.com/data/error.apsx?file=123456.mp3 系统将用户发送到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. 我需要这个来处理大型系统应该提供mp3文件的方式。

If a user tries to access myapp.com/otherFolder/notHere.whatever, the system returns the standard 404 error normally. 如果用户尝试访问myapp.com/otherFolder/notHere.whatever,则系统会正常返回标准404错误。

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. 我知道在IIS中有一些方法可以指定,但是如果我能以编程方式执行某些操作或者只是使用我的.net项目,我会喜欢它。

edit: 编辑:

I've created a web.config file in myapp.com/data/ 我在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 doesn't seem to be working. 这似乎不起作用。

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

In your web.config. 在你的web.config中。

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

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. 您要做的第一件事是确保ASP.NET能够处理这些文件请求,因为默认情况下.mp3不是ASP.NET扩展,这只是由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. 第一个是创建一个HttpModule,它监视OnUnhandledException事件。 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. 由于ASP.NET将404(以及所有HTTP错误)抛出为HttpException类型异常,因此模块将为您提供捕获,解析请求和重定向到您自己的目的的位置。

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. 另一种方法是在你关心的文件夹级别创建一个web.config(这些可以嵌套记住)并在那里创建customerror部分。 This is more straightforward but affords much less control. 这更直接,但提供的控制要少得多。 All things considered I would favour the module generally. 考虑到所有事情,我一般都赞成这个模块。

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 可能你可以把web.config放在你想要输入特定错误的文件夹中,并在webconfig中输入带有指示页面的customerror标记,或者你可以使用主web.config中的location标记。

but i'm not sure about this 但我不确定这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM