c# - URL where user come from ASP.Net but have NULL -


i want know-from url user come from. so, use

uri myurl = request.urlreferrer; 

but when null value myurl:

i have 2 projects-first aspx page, second- redirects first project-page parameters. when second project redirect first project- have :

object reference not set instance of object.

my second test project simple:

 protected void page_load(object sender, eventargs e)     {         response.redirect("http://localhost:54287/go.aspx?id=default");     } 

first , main project:

 protected void page_load(object sender, eventargs e)     {         //request.servervariables('http_referer');          //  request.servervariables;          string id = request.querystring["id"];          if (id != null)         {          uri myurl = request.urlreferrer;                  console.writeline(myurl);                 response.write("referrer url : " + myurl.absolutepath);         }     } 

error in :response.write("referrer url : " + myurl.absolutepath);

ok, there a few errors:

your code:

uri myurl = request.urlreferrer; console.writeline(myurl); response.write("referrer url : " + myurl.absolutepath); 

in code above nullreferenceexception because myurl null.

the urlreferer may null, have check like:

uri myurl = request.urlreferrer; console.writeline(myurl); if (myurl != null)    response.write("referrer url : " + myurl.absolutepath); 

also can never make sure urlreferer can have value, if user comes website don't know if website provide value, have first assume referrer null (in summary never trust it).

second, when use response.redirect on code ran server-side don't know referrer.

i find this question , this question better understand.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -