简体   繁体   中英

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

  • 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.

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

In your 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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