URLs, 404 redirect etc.

D

David

Hi all,

Using C#.NET 1.1

I am progressing on my URL ReWriting quite well, but have a slight problem.

My server will not have real paths, just virtual, which is generated from a
database. This will include images, documents etc. (The images will map (URL
ReWrite) to real images of a different name.)

I am using the IIS 404 error to redirect to an ASPX page, which will then
allow my system to handle everything that does not exist on the file system.
This is working fantastically, though I do have a slight niggle with it.

If I call a virtual image (i.e. I don't have the real file, but I will URL
ReWrite to the correct image), my 404 handler gets called (this is what I
expect). As the handler is .ASPX, my URL ReWrite code in global.asax.cs gets
called. (Again, this is what I expect). I then rewrite the call to the image
to the real image that will be displayed. (This again is working like a
dream). However, what I didn't expect is the 404 handler page (even though
it doesn't actually get hit) is recorded in the web log files as the
requested item.

Trying to resolve this, I have passed the URL Rewriter to call the 404 page
direct and pass a parameter in the querystring. In my 404 page, I do a
response.redirect to the image that I want. This sort of works, but I get
the 404 page with a 304 response AND the rewritten image recorded in the log
files. I can live with the rewritten image (not the called image name) but I
don't want the 404 page to show up in my logs.

So, I tried a server.transfer inside the 404 page. This shows the image, but
it records the 404 page in the logs.

I am looking for ideas as to how to get around this problem.

In summary...
If I call an image (which needs to be URL ReWritten), I want the image to be
recorded in the log. This works fine if I call aspx pages (though not yet
tried it for classic asp, but really, I am not so interested in that at the
moment).

Any ideas?

BTW. I cannot do anything that will need to be installed on a server. This
has to be copy and paste deployment (i.e. can be run on a shared server at
an ISP.), so has to be handled within .NET.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
G

Guest

I am looking for ideas as to how to get around this problem.

Of course, it does 404 entry in the log because IIS doing it before
your see the page.

You should forget about old 404-style-redirects in .Net, use
HttpModule instead.

Example:

using System;
using System.Web;

namespace SomeModule
{
public class MyModule: IHttpModule
{
#region IHttpModule Members
public void Init(HttpApplication context)
{
context.BeginRequest += new
EventHandler(context_BeginRequest);
}

public void Dispose() { }
#endregion

private void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
if (app.Context.Request.Url.ToString() == "....old.jpg)
app.Context.RewritePath("new.jpg");

}
}
}


-- in the web.config ---

</httpModules>

After that all requests to old.jpg will be rewrited to new location
and you will have Status 200 in your IIS log.
 
G

Guest

Hi,

Thanks for this...

The old style 404 redirect does work if I call an unknown page, but for some
reason, not my images. My page name gets logged in the log files.

The one that you are showing here... how does that hook into the IIS
application? (I do not know the answer to this, so this is a genuine
question) How can .NET trap for unknown files?

What I also need to do (which currently does work) is to be able to rewrite
files with no extension (for example, a directory). Will this HttpModule work
for that?

Thanks.

Regards,
Dave Colliver.
http://www.LiverpoolFOCUS.com
~~
http://www.FOCUSPortals.com - Portal franchises available
 
G

Guest

On Feb 15, 10:25 am, David Colliver

David, I'm sorry, I forgot that for images (and files with no
extension) you have to register the extension within IIS (what you
don't want to do).

There are many articles about custom httpmodules and httphandlers
(where you can handle the requests based on the type of file extension
used)

You should check this one
http://msdn2.microsoft.com/en-us/library/ms972974.aspx

But using httpmodules and httphandlers for the other files (not
*.aspx, or similar) required to register the extension within IIS and
ASP.NET
 
G

Guest

What I also need to do (which currently does work) is to be able to rewrite
files with no extension (for example, a directory). Will this HttpModule work
for that?

P.S.

For links such as http://site/Dir/Dir/Dir/PageDoesNotExist.aspx this
HttpModule will work.

If you don't want to change anything in IIS, this HttpModule will work
for files with no extension if you created a directory with the same
name and empty default.aspx in it.

For example, to rewrite URL http://site/PageDoesNotExist to another
page you need to create a directory named PageDoesNotExist and put a
file default.aspx into this directory.
 
G

Guest

Hi,

Thank you.

I am not currently using httpmodule, but have my current re-write code
inside my application_beginrequest of global.asa.

I am aware of the issues of having no real pages or folders, which is why I
am handling through the IIS 404 handler. What I didn't know is if you knew
something I didn't, to which I continued my question.

The app I am building has to be done in a virtual way (i.e. virtual files
and folders rather than real files and folders). The current IIS way of
handling it works brilliantly apart from the log file. :-(

If anyone has any ideas of getting the correct file listed in the log file
instead of my redirect handler, I would very much appreciate it.

Thanks.

Regards,
Dave Colliver.
http://www.SheffieldFOCUS.com
~~
http://www.FOCUSPortals.com - Portal franchises available
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top