How can I write an if statement based on the current page? I need to have one that says: If not on This Page
redirect here
. There is more to it to check who is logged in, but that part is already handled. Just trying to limit access to one page.
Asked
Active
Viewed 409 times
0
-
Webforms? MVC? Some other kind of page? – Joel Coehoorn Feb 06 '18 at 20:39
-
@JoelCoehoorn Sorry. Webforms. Updated my tags. – Cole Feb 06 '18 at 20:51
-
Is this part of a master page? And what is the context where you do this: in the code behind? Which page event? – Joel Coehoorn Feb 06 '18 at 20:53
-
Also adding the asp.net tag. Some C#'ers here will jump in on asp.net vb questions, but they won't know about it unless they see that tag. – Joel Coehoorn Feb 06 '18 at 20:54
-
1Your question lacks a lot of info and is difficult to understand. What are the conditions for blocking access? A user role, a specific url, a certain time of day, the alignment of the stars? – VDWWD Feb 06 '18 at 20:57
-
@VDWWD I just want to know how I can use specific a page in an if statement. As in if on "website/thispage.apsx" then redirect. Specific url is fine if that is the way to do it. I don't have any clue – Cole Feb 06 '18 at 21:03
1 Answers
1
You might want to do something like this
if (Request.Url.AbsoluteUri.Contains("Page1.aspx"))
{
//do something
}
if (Request.RawUrl.ToLower().Contains("partofurl"))
{
//do something
}
Request.Url.AbsoluteUri
gives you the full url
https://stackoverflow.com/questions/48651567
But Request.RawUrl
only gives the path without the domain
/questions/48651567
But there are a lot more Request
options, see this for more.

VDWWD
- 35,079
- 22
- 62
- 79