marcgg
marcgg

Reputation: 66445

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:

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.

Upvotes: 1

Views: 965

Answers (3)

annakata
annakata

Reputation: 75844

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.

Upvotes: 2

Omu
Omu

Reputation: 71206

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

Upvotes: 0

madcolor
madcolor

Reputation: 8170

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

In your web.config.

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

Upvotes: 2

Related Questions