Redirects with ASP.NET 2.0 and wildcard mappings

Discussion in 'ASP .Net' started by SlimFlem, Dec 21, 2005.

  1. SlimFlem

    SlimFlem Guest

    I have searched hard for 2 days on this and keep hitting a wall. I
    have a custom IHttpHandler setup to do Url mappings to prop up some old
    Urls for our site. I have also created a wildcard mapping in IIS6
    using the ASP.NET 2.0 ISAPI DLL.

    Here is what I am attempting and I cannot for the life of me figure
    this out.

    I have several situations:

    1) Remap Urls configured in a .Xml file using my IHttpHandler and build
    them using custom infrastructure.
    2) Allow Urls that do not require a remapping to "pass thru" and
    resolve as normal.
    3) Still server normal static content. (asp.net 2.0 isapi will allow
    this)

    I know that the asp.net 2.0 ISAPI DLL behaves properly when using it as
    a wildcard mapping in that it will not prevent static content from
    being served, etc. This I have verified.

    In my web.config, if I setup a handler for verb="*" and type="*" for my
    IHttpHandler in addition to using the wildcard mapping, this pushes all
    requests thru to me. However, what I am missing is how to
    programatically or otherwise, temporarily disable my custom
    IHttpHandler so I can allow a Url to pass thru that does not require
    remapping. Does this make sense?

    The reason I am using a wildcard mappings is because I want to remap
    Urls that have paths that do not physically exist within the website.
    I am having a hard time getting both sides of this to work together.

    Any tips would be greatly appreciated.
     
    SlimFlem, Dec 21, 2005
    #1
    1. Advertisements

  2. SlimFlem

    Brock Allen Guest

    However, what I am missing is how to
    This is based upon the static .config file mappings. So there's no public
    API that I have ever found to change this at runtime. One approach that might
    help is to call HttpContext.RewritePath prior to the HttpApplication's MapHandler
    execution step (which comes inbetween ResolveRequestCache and AcquireRequestState).
    There are problems with RewritePath, but it might work for you.

    The other approach is to simply have your handler act as a pass-thru for
    another IHttpHandler. So in your ProcessRequest you dynamically load someother
    handler and call into its ProcessRequest.
     
    Brock Allen, Dec 21, 2005
    #2
    1. Advertisements

  3. You might be thinking just a bit too deep here by trying to intercept each
    request with a handler when a few lines in the onBeginRequest method of an
    ihttpmodule would probably suffice.

    This is a likely route for evaluating the request path: For all requests
    intercept the path in onBeginRequest and make sure its not a path that needs
    no remapping - simply do nothing with them, and for those that need
    remapping issue a response.redirect in the module (not a transfer). For
    pages that dont exist, well they are also paths you should do nothing with
    but you should implement a global error handler, let the page not found
    error happen for any pages that do not exist and do not need remapping and
    redirect in the error handler to the path you choose for any page that does
    not exist. That should cope for each scenario you have described with one
    caveat, files that do not exist should technically return a 404, you have to
    watch for erros passed for things like not found images when globally
    handling this and decide how you'll cope with that.
     
    John Timney \( MVP \), Dec 21, 2005
    #3
  4. SlimFlem

    SlimFlem Guest

    Thanks for the tips. What I'm actually doing is building all pages
    dynamically using an in-house CMS system, so I have one generic page
    that pieces together content and renders a final output page. The
    reason I choose a custom handler is because from what I've read, this
    is the way to go for this. Here are some code snips of what I am
    currently doing and attempting.

    When I determine the request *does not* need remapping, I do this
    inside an if..

    IHttpAsyncHandler theHandler = new DefaultHttpHandler();
    theHandler.BeginProcessRequest(context, null, null);
    return;

    and finally, when I am redirecting/rendering a dynamic page, I do
    this...

    string pageLoaderVirtualPath =
    ConfigurationManager.AppSettings["pageLoaderVirtualPath"];
    string pageLoaderPhysicalPath =
    context.Server.MapPath(pageLoaderVirtualPath);

    // get an instance to the generic page loader aspx page
    IHttpHandler handler =
    PageParser.GetCompiledPageInstance(pageLoaderVirtualPath,
    pageLoaderPhysicalPath, context);

    // process the redirected request
    handler.ProcessRequest(context);
    context.ApplicationInstance.CompleteRequest();


    of course there is other stuff I'm doing for making my decisions. The
    piece I put that passes the request to an instance of
    DefaultHttpHandler seems to be working most of the time. I am trying
    this based on Brock's previous post.

    I am new to some of these ways of building pages and using
    IHttpHandlers in general, so it's trial and error right now.

    thanks for the tips and any advice.
     
    SlimFlem, Dec 21, 2005
    #4
  5. SlimFlem

    SlimFlem Guest

    The stuff I posted in my last post appears to be working and doing what
    I want it to do, except this code:

    IHttpAsyncHandler theHandler = new DefaultHttpHandler();
    theHandler.BeginProcessRequest(context, null, null);
    return;

    Does not allow aspx pages to be processed for Url's that do no require
    remapping, ie: the pass-thru Url's. When running in the debugger for a
    Url point to a .aspx that exists on disc and is not configured to be
    remapped, my IHttpHandler gets called over and over and over after
    hitting the code lines I pasted in above. If the Url is not for an
    aspx, then it processes fine. When not running in the debugger, the
    browser just says the page cannot be displayed.

    Any ideas? I can paste more code snippets if needed.
     
    SlimFlem, Dec 22, 2005
    #5
  6. SlimFlem

    SlimFlem Guest

    I now understand what is happening. Since I have registered an
    IHttpHandler in web.config such as:

    <add verb="*" path="*" type="MyType"/>

    and I am passing control back to DefaultHttpHandler for IIS to process
    when the Url does not require remapping, IIS immediately sends it back
    to ASP.NET and my handler catches the request again and the cycle
    continues.

    Can I somehow solve this using an IHttpModule instead but still allow
    the equivalent of the following code to work?

    IHttpHandler handler =
    PageParser.GetCompiledPageInstance(pageLoaderVirtualPath,
    pageLoaderPhysicalPath, context);
    handler.ProcessRequest(context);
    context.ApplicationInstance.CompleteRequest();
     
    SlimFlem, Dec 22, 2005
    #6
  7. SlimFlem

    SlimFlem Guest

    John, I took your advice and I now have this working like a champ. I
    disable my IHttpHandler and moved my code, slightly modified, into an
    IHttpModule. Now all remapping occurs and stuff that should not be
    remapped falls around an If statement and is handled normally. THanks.
     
    SlimFlem, Dec 22, 2005
    #7
  8. Glad to have helped.
     
    John Timney \( MVP \), Dec 22, 2005
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.