8

Possible Duplicate:
How to get the URL of the current page in C#

If I am in a page say http://myweb/folder/obtain.aspx?thevalue=3 , how can i determine if the url contains obtain.aspx?thevalue in c#?. I just need to check whether the user landed on this particular page.

PS: I guess I dont really need to check for the ?thevalue but just the obtain.aspx

Community
  • 1
  • 1
user710502
  • 11,181
  • 29
  • 106
  • 161
  • @RichardD The answers there do not explain how to get the name of the actual page/file landed on, they just show how to get the Url. Based on title they are the same, but question content makes them different. – Nate Nov 18 '11 at 16:12
  • How to do this in python? – OmK Apr 05 '18 at 08:56

6 Answers6

12

Try this:

//gets the current url
string currentUrl = Request.Url.AbsoluteUri;

//check the url to see if it contains your value
if (currentUrl.ToLower().Contains("obtain.aspx?thevalue"))
    //do something
James Johnson
  • 45,496
  • 8
  • 73
  • 110
2

This will give you the exact file name ( obtain.aspx ) Request.Url.Segments[1]

Alex Z
  • 1,362
  • 15
  • 16
  • In my case it's the last segment which is 3. How would I modify the above to get the end of the URL (the last part after /)? – Naomi Apr 12 '18 at 22:34
1

Request.Url should contain everything you need. In your case, you could use something like

if( Request.Url.PathAndQuery.IndexOf("obtain.aspx") >= 0 )...
Doozer Blake
  • 7,677
  • 2
  • 29
  • 40
1

I recommend using Request.Url. To get the exact file name, you may try also using System.IO.Path

var aspxFile = System.IO.Path.GetFileName(Request.Url.LocalPath);
var landed = aspxFile.Equals("obtain.aspx", StringComparison.InvariantCultureIgnoreCase);
if(landed) { // your code }
Nate
  • 30,286
  • 23
  • 113
  • 184
0

Request.Url will return the exact Uri being requested by the user.

If you want to check specifically for thevalue, you're probably better off looking for that in Request.QueryString

Dan Herbert
  • 99,428
  • 48
  • 189
  • 219
0

it's ugly but you cand try

if (HttpContext.Current.HttpRequest.Url.AbsolutePath.Contains("/obtain.aspx"))
 // then do something
Tomasz Jaskuλa
  • 15,723
  • 5
  • 46
  • 73
  • 1
    Contains will give a true for "2obtain.aspx" aswell... Equals() should be used & System.IO.Path.GetFileName(Request.Url.LocalPath); – Kim Nov 18 '11 at 16:16