« Previous Next »

Answered Thread: Trouble rewriting urls to static content

Last post 10-24-2009 2:59 AM by anilr. 1 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (2 items)

Sort Posts:

  • 10-23-2009, 11:35 PM

    • tmkadlec
    • Not Ranked
    • Joined on 10-24-2009, 3:02 AM
    • Posts 1

    Trouble rewriting urls to static content

    I'm attempting to write, what i thought would be, a simple HttpModule to rewrite incoming requests like "/styles/{somerevisionnumber}foo.css" to a static file at "/styles/foo.css".  (The purpose being to insert a file hash or file modified date values in place of {somerevisionnumber} when generating urls in my web application to force browsers to retrieve updated files immediately while being able to set my expiration caches far into the future.)

    The key requirement I can't seem to accomplish is that after the url is rewritten I want the request to go through just as it would for any static file.  Meaning I want the StaticCompressionModule to compress and cache the file once on the first request and return the cached gzipped file on future requests for these redirected urls.

    (Note: I'm attempting this under Windows 7, IIS 7.5, .Net 4, Visual Studio 2010 Beta 2)

    For experimental purposes, in my BeginRequest handler I have something that effectively is:

    if(Context.Request.Path == "/styles/1foo.htm") {

    Context.RewritePath("/styles/foo.htm");

    }

    The act of calling Context.RewritePath seems to make further processing of the request 'dynamic'.  i.e. I've noticed if I enable dynamic HttpCompression I will get a gzipped response from 1foo.htm but having *only* static HttpCompression turned on gives me uncompressed responses for 1foo.htm but compressed responses for foo.htm.

    Even if i were to change my code to:

    if(Context.Request.Path == "/styles/foo.htm") {

    Context.RewritePath("/styles/foo.htm");

    }

    Notice i'm 'rewriting' to the exact same url.  The act of calling Context.RewritePath alone seems to cause the request not to go through the static file / compression modules.

    Admittedly i don't understand a lot of intricacies going on here.  Is this pipeline ordering issue?  Do I need to specify my url rewriting module run earlier?  Am i using the wrong technique to rewrite the url?

    As I mentioned I thought this would be simple and  I would really like to gain a better understanding of what is going on and how to accomplish this.  Thanks for your help!

     

    Thomas

  • 10-24-2009, 2:59 AM In reply to

    • anilr
    • Top 10 Contributor
    • Joined on 05-23-2006, 6:13 PM
    • Redmond, WA
    • Posts 2,343

    Answered Re: Trouble rewriting urls to static content

    RewritePath does not do enough rewriting to be useful for non-PageHandlerFactory requests - you need to use Server.TransferRequest.

    Anil Ruia
    Senior Software Design Engineer
    IIS Core Server
Page 1 of 1 (2 items)
Microsoft Communities