views:

104

answers:

3

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.

+2  A: 

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

In your web.config.

<customErrors
       mode="RemoteOnly" 
       defaultRedirect="~/errors/GeneralError.aspx" 
/>
madcolor
Would it work with all files or just aspx files?
marcgg
all files not found.. the generalerror.aspx is the display page.
madcolor
Also, this would work for the whole application and not only for a folder. I want to be able to specify on what folder it should apply
marcgg
You can put a web.config in any folder which will override.
madcolor
If Omu's answer is correct and you can put a web config in a file then it would work I suppose. I'm not sure how I should go and do this.
marcgg
http://www.codeproject.com/KB/aspnet/multipleWebConfig.aspx
madcolor
Thanks a lot. I edited the question with what I did, it doesn't seem to work
marcgg
Are you viewing it on the server?
madcolor
You can switch mode to "on" if you're not being redirected.
madcolor
Not sure what you mean. I'm seeing it on the server I have locally (asp.net development server)
marcgg
it fixed it! (mode = on) Thanks! Do you know why remoteOnly was breaking it?
marcgg
RemoteOnly means that you (viewing locally) will see the error, but your audience (remote) will see the nice happy redirect.
madcolor
Also, I need the original file that have been called (in my example myapp.com/data/123456.mp3), can I retrieve it from GeneralError.aspx?
marcgg
Oh nevermind it looks like it's in the url : errorpath=/data/fsdfsd.mp3
marcgg
Well madcolor, thanks a lot for your help, I really appreciate it. I'm really glad I can do it that way :)
marcgg
If you want even more control you can write an HTTPmodule to do more manipulation.
madcolor
A: 

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

Omu
+1  A: 

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.

annakata
I don't need control since it's 100% sure that this action will only be in this folder and will only do this one action. So I suppose I need to use the web config. I edited my question qith what I did
marcgg