Problem with URL rewriting in IHttpModule

Discussion in '.NET' started by mark.norgate, Mar 31, 2009.

  1. mark.norgate

    mark.norgate

    Joined:
    Mar 31, 2009
    Messages:
    2
    Hello

    I have implemented an IHttpModule that rewrites user-friendly paths to actual file resources. However, since my user-friendly paths have no extensions, such as:

    ~/mysite/my-friendly-name

    ...the session object in the context within my PostAcquireRequestState event handler is empty. This is not a problem; I simply assign a dummy handler to my request in a PostMapRequestHandler event handler, like this:

    if (contentPage != null && handler.GetType() == typeof(System.Web.DefaultHttpHandler))
    HttpContext.Current.Handler = new EnforceSessionStateHandler();

    Where EnforceSessionStateHandler looks like this:

    public class EnforceSessionStateHandler : IHttpHandler, IRequiresSessionState
    {
    public bool IsReusable
    {
    get { throw new NotImplementedException(); }
    }

    public void ProcessRequest( HttpContext context )
    {
    throw new NotImplementedException();
    }

    }

    It is within my PostAcquireRequestState method handler that I am actually doing the rewrite. Previously, I have been using Server.Transfer() to do the rewrite, which worked fine until I started adding user controls to the page. Then I discovered that the post back data was being lost because of the transfer - none of the events of my controls were being raised (such as an OnClick for a button).

    So, I changed the Server.Transfer() to a HttpContext.RewritePath(). However, this invokes the ProcessRequest90 method of my dummy EnforceSessionStateHandler handler, which of course throws an exception.

    So I think I have three choices:

    1) I find a means to acquire the state in the PostAcquireRequestState and use HttpContext.RewritePath() without having to create a dummy handler;

    2) or, I somehow dynamically create the page using the Activator class - unfortunately the page's class is not available via reflection because it's not being compiled into an assembly during debugging;

    3) or, I take the post back state from the request object and somehow inject this into my Server.Transfer().

    Does anyone have any ideas how I could proceed with this?

    Regards, Mark
    mark.norgate, Mar 31, 2009
    #1
    1. Advertising

  2. mark.norgate

    mark.norgate

    Joined:
    Mar 31, 2009
    Messages:
    2
    Temporary solution...

    Ok,

    FYI, I think I've found a problem, but I'm not entirely happy with it. I think it's rather slow. I decided on option 2 above, like this: if (contentPage != null && handler.GetType() == typeof(System.Web.DefaultHttpHandler))
    {
    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

    bool exitFor = false;

    foreach (Assembly assembly in assemblies)
    {
    Type[] types = assembly.GetTypes();

    foreach (Type type in types)
    {
    if (type.Name == "content_aspx")
    {
    HttpContext.Current.Handler = (IHttpHandler)Activator.CreateInstance(type);
    exitFor = true;
    break;
    }
    }

    if (exitFor) break;
    }
    }

    Does anyone have any idea how I can improve this?

    Thanks, Mark
    mark.norgate, Mar 31, 2009
    #2
    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. KMA

    URL rewriting problem

    KMA, Mar 17, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    388
    Sonu Kapoor [MVP]
    Mar 17, 2006
  2. Gaurav  Agarwal
    Replies:
    2
    Views:
    710
    Gaurav Agarwal
    Jan 31, 2005
  3. river13
    Replies:
    0
    Views:
    363
    river13
    Nov 14, 2006
  4. =?Utf-8?B?cmFwaWRkYXRh?=

    url rewriting with no extension in url

    =?Utf-8?B?cmFwaWRkYXRh?=, Nov 1, 2006, in forum: ASP .Net
    Replies:
    8
    Views:
    391
    Flinky Wisty Pomm
    Nov 2, 2006
  5. Fernando Gómez

    Determine request URL when implementing IHttpModule

    Fernando Gómez, Jul 16, 2009, in forum: ASP .Net
    Replies:
    0
    Views:
    338
    Fernando Gómez
    Jul 16, 2009
Loading...

Share This Page