Check if URL variable exists

I've never written a line of C#, so don't laugh at me here. All I need to do is check to see if a URL variable exists, if so I need to output a few lines of javascript. How shall I do so?

Thank you
elmoredanielAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

orbulatCommented:
1. drag a hidden variable in design mode, eg. <input type=hidden id=hidURL runat=server />
2. in your code behind add this function for checking url, this function may run a little bit slow

public bool checkURL(string url)
{
   try
   {
      HttpWebRequest hReq= (HttpWebRequest)WebRequest.Create(url);
      HttpWebResponse hResp = (HttpWebResponse)hReq.GetResponse();
      hResp .Close();
      return true;
    }
    catch
    {
       return false;
    }
}

3. in Page_Load

add the following:

if (!checkURL("http://www.your_url.com"))
{
     hidURL.value = "false";
}

4. in your javascript:

if (document.Form1.hidURL.value == "false")
   alert("url is invalid!");
elmoredanielAuthor Commented:
Humm, don't know if that's what I needed, maybe I'm wrong.

I have a javascript tag and if the page is accessed with "admin" in the query string ie. (page.aspx?admin)  Then I need to output:  a javascript alert.

So can't C# do something like:

<script language="javascript">

if(url_variable_admin_exists ...)
   alert('the variables exists');

</script>
irps20001Commented:
Hi,
You can get the url with the help of Request.
For instance, say

String URL = Request.Path.ToLower();

now you have the complete url with you.
URL has got different segments. You can get all the parts using Segments and use it as per your requirement

do let me know if you have further doubts so that i can help you on this
HTH

Happy Programming
Rana

OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

GavinMannionCommented:
So you want to check what is in the URL?

Does it really look like page.aspx?admin or can you make it look something like page.aspx?usertype=admin

If you can make it look this my second option then in your codebehind do this

string userType = Request.QueryString["usertype"]
if( userType == "admin")
{
      this.RegisterStartupscript("alert","<script language='javascript'>alert('the variable is admin');</script>");
}

irps20001Commented:
Use "Request.Url.Segments" to fetch different parts of the URL
Rana
elmoredanielAuthor Commented:
Apparently no one read my question, because it says "I've never written a line of C#"   and I will never have to again after this.

GavinMannion your code looks the most promising. But I don't know what a codebehind is. Can't I place this code directly in the page. Also, it looks like your code requires the url variable "usertype" to exists, which it may not exist.

Thank you
GavinMannionCommented:
Okay you can put it on the page inside scripting brackets.

<%
string userType = Request.QueryString["usertype"]
if( userType == "admin")
{
      this.RegisterStartupscript("alert","<script language='javascript'>alert('the variable is admin');</script>");
}
%>

If the usertype variable does not exist then it doesn't matter. It will then find nothing and do nothing.
The normal way for URL params to work is URL?ParamName=ParamValue

AS long as this structure is there then my code whould work. If you don't have this type of structure then it won't. if the URL looks like

mypage.aspx?usertype=

Then my code will still work

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.