URLs, 404 redirect etc.

Discussion in 'ASP .Net' started by David, Feb 14, 2007.

  1. David

    David Guest

    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
     
    David, Feb 14, 2007
    #1
    1. Advertising

  2. On Feb 14, 11:20 pm, "David"
    <> wrote:
    >
    > 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>
    <add name="MyModule" type="SomeModule.MyModule, SomeModule" /
    >

    </httpModules>

    After that all requests to old.jpg will be rewrited to new location
    and you will have Status 200 in your IIS log.
     
    Alexey Smirnov, Feb 14, 2007
    #2
    1. Advertising

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


    "Alexey Smirnov" wrote:

    > On Feb 14, 11:20 pm, "David"
    > <> wrote:
    > >
    > > 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>
    > <add name="MyModule" type="SomeModule.MyModule, SomeModule" /
    > >

    > </httpModules>
    >
    > After that all requests to old.jpg will be rewrited to new location
    > and you will have Status 200 in your IIS log.
    >
    >
     
    =?Utf-8?B?RGF2aWQgQ29sbGl2ZXI=?=, Feb 15, 2007
    #3
  4. On Feb 15, 10:25 am, David Colliver
    <> wrote:

    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
     
    Alexey Smirnov, Feb 15, 2007
    #4
  5. On Feb 15, 10:25 am, David Colliver
    <> wrote:

    > 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.
     
    Alexey Smirnov, Feb 15, 2007
    #5
  6. 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


    "Alexey Smirnov" wrote:

    > On Feb 15, 10:25 am, David Colliver
    > <> wrote:
    >
    > > 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.
    >
    >
     
    =?Utf-8?B?RGF2aWQgQ29sbGl2ZXI=?=, Feb 15, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kaidi
    Replies:
    5
    Views:
    475
    Andrew Thompson
    Jan 4, 2004
  2. Kevin Walzer

    Re: PIL (etc etc etc) on OS X

    Kevin Walzer, Aug 1, 2008, in forum: Python
    Replies:
    4
    Views:
    413
    Fredrik Lundh
    Aug 13, 2008
  3. Nathan Sokalski

    Converting Relative URLs into Absolute URLs

    Nathan Sokalski, Aug 11, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    749
    Sriram Srivatsan
    Aug 12, 2008
  4. Adam Monsen

    JDBC URLs ...not really URLs?

    Adam Monsen, Feb 6, 2009, in forum: Java
    Replies:
    11
    Views:
    6,227
    Adam Monsen
    Feb 8, 2009
  5. Steve T.

    dynamic URLS convert to static URLS for search engines

    Steve T., Mar 1, 2004, in forum: ASP .Net Web Services
    Replies:
    7
    Views:
    293
    Steve T.
    Mar 4, 2004
Loading...

Share This Page