16

I am working on a web app which makes use of a 3rd party HttpModule that performs url rewriting.

I want to know if there's any way to determine the original url later on in Application_BeginRequest event. For example...

Original url:
http://domain.com/products/cool-hat.aspx

Re-written url (from 3rd party httpmodule):
http://domain.com/products.aspx?productId=123

In the past I have written HttpModules that store the original url in HttpContext.Items but, this is a 3rd party app and I have no way of doing that.

Any ideas would be appreciated.

jessegavin
  • 74,067
  • 28
  • 136
  • 164

4 Answers4

29

Try this:

string originalUrl = HttpContext.Current.Request.RawUrl;

The original URL is inside of this property.

Colin Breame
  • 1,367
  • 2
  • 15
  • 18
10

I had the same problem, but I wanted the fully qualified URL (RawUrl gives you just the Path and Query part). So, to build on Josh's answer:

string originalUrlFull = 
   Page.Request.Url.GetLeftPart(System.UriPartial.Authority) + 
   Page.Request.RawUrl
ThisGuy
  • 2,335
  • 1
  • 28
  • 34
7

I know this question was asked long time ago. But, this is what I use:

System.Uri originalUri = new System.Uri(Page.Request.Url, Page.Request.RawUrl)

Once you have the URI you can do a ToString() to get the string, or cal any of the methods/properties to get the parts.

crash
  • 197
  • 1
  • 3
3

Create a new HttpModule to serve as a wrapper around (inherits) the third party module and do whatever you want with it.

In your case, override the appropriate function (ProcessRequest?) and store the original url in HttpContext.Items, and then call the MyBase implementation. Should work fine.

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
  • 7
    Why would you go with all this trouble... ? Simply use RawUrl property of the Request. Much much much simpler than all this module trouble. Module also has its overhead... – Yuki Aug 21 '13 at 12:18
  • @Yuki, what if the original URL has a different domain??? – Sergey Nudnov Apr 21 '22 at 15:56