0

I have a feeling I'm using HttpContext.Current incorrectly, based on some strange code output.

I have two pages, http://example.com/Foo.aspx, and http://example.com/Bar.aspx. Both have a button that, when clicked, calls a static function in a class outside of the Page's context that emails me System.Web.HttpContext.Current.Request.Url.AbsolutePath as that static function sees it.

I would expect that when the button on Foo.aspx is sent, it would email me "/Foo.aspx" ... and usually it does. We noticed one time where that call from Foo.aspx sent us "/Bar.aspx" unexpectedly.

Both pages could have conceivably been loaded by the same client.

  • What's going on here? How does HttpContext.Current.Request return a different path than the page that called the static function that references it?
  • How can I get what I want - i.e., unfailingly get the absolute path of the page that called the static function that cares about it?
Chris
  • 4,030
  • 4
  • 47
  • 76

2 Answers2

2
  • The way I understand it, you are using the current context correctly. It should always be the url that was requested. If you did something like Server.Transfer, you might be able to end up in a situation where the url doesn't match that of the currently executing page (but I don't think I've tried this).
  • You can have the page pass in its own name.
bmm6o
  • 6,187
  • 3
  • 28
  • 55
  • +1 for having the page pass its name as an argument to the function. – Steve May 24 '12 at 15:23
  • I'm thinking of passing the static function the HttpContext.Current object directly from the page to ensure it's got the correct one. – Chris May 24 '12 at 16:42
0

HttpContext.Current.Request will return the correct path for the thread that is processing your request (even if ASP.NET switches the request processing to another thread). Are you sure that you are calling it in the same thread? And are you 100% sure that somehow you didn't get another request coming in for Foo.aspx at the same or similar time?

Martin Ernst
  • 5,629
  • 2
  • 17
  • 14
  • Statics are shared across threads, so it doesn't matter which thread is interacting with the object. I believe that ASP.NET isolates requests across AppDomains and so they cannot interfere with each other unless you do something specific to allow cross domain communication. – Steve May 24 '12 at 15:21
  • 1
    HttpContext.Current is a threadstatic (ie. static for any access to it on the same thread), not a static, and the ASP.NET runtime sometimes switches the thread that long running requests are using, and in that case the ASP.NET runtime will also update the threadstatic value of HttpContext.Current. – Martin Ernst May 24 '12 at 15:25
  • I looked into it more and you are correct. HttpContext.Current does look up into thread specific storage. – Steve May 24 '12 at 15:46