I am using C#. Below is my sample code.
private void Page_Load(object sender, System.EventArgs e)
{
string str = Request.UrlReferrer.ToString();
Label1.Text = str;
}
The result in Label1.Text is http://localhost:82/data/WebForm1.aspx.
Now I want the result "WebForm1.aspx" in Label1.Text
can you please help me?
Thanks.
If you want only the part after the last / in the URL, calling the System.IO.Path.GetFileName() method on the Uri.LocalPath should do the trick:
System.IO.Path.GetFileName(Request.UrlReferrer.LocalPath);
If you want the output to keep query string information from the URI, use the PathAndQuery property:
System.IO.Path.GetFileName(Request.UrlReferrer.PathAndQuery);
Try the LocalPath property on the UrlReferrer:
Label1.Text = Request.UrlReferrer.LocalPath;
It should provide you with just the filename.
Edit: this seems to also include the path, so only works for root.
In which case, you're better off just using Substring():
string str = Request.UrlReferrer.ToString();
Label1.Text = str.Substring(str.LastIndexOf('/')+1);
Related
I've been trying to get SetAttribute with the webBrowser.Document for a while, and for some reason the webBrowser fields don't get filled in.
I'm certain that I have the ID's correct, but perhaps i'm just overlooking something. I am setting textfields for the netflix login website.
My code is as follows:
var emailField = webBrowser1.Document.GetElementById("email");
var passField = webBrowser1.Document.GetElementById("password");
emailField.SetAttribute("value", username);
passField.SetAttribute("value", password);
Thanks for any help, the username is a string which includes the email, and password contains the password, also in a string.
You should put your code in DocumentCompleted.
Example
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var emailField = webBrowser1.Document.GetElementById("email");
var passField = webBrowser1.Document.GetElementById("password");
emailField.SetAttribute("value", "123");
passField.SetAttribute("value", "456");
}
Try it again.
It's the first time I'm passing variables between two pages in my asp.net project.
It works, but I'm wondering if it is a good way to do it? Is it secure? Is there a better way?
The reason why I ask is that I've have learned never to use concatenation in sql, but instead use parameters (which I always do from now on). Is there a similar risk in this case?
In web page1:
protected void Button1_Click(object sender, EventArgs e)
{
string email = txtEmail.Text;
string date = txtDate.Text;
string time = txtTime.Text;
string name = txtName.Text;
string url = "~/change.aspx?newemail="+mail+"&newdate="+date+"&newtime="+time+"&newname="+name+"";
Response.Redirect(url);
}
In web page2:
protected void Page_Load(object sender, EventArgs e)
{
String email = Request.QueryString["newemail"];
String date = Request.QueryString["newdate"];
String time = Request.QueryString["newtime"];
String name = Request.QueryString["newname];
TextBox1.Text = email;
TextBox2.Text = date;
TextBox3.Text = time;
TextBox4.Text = name;
}
if it is a good way to do it?
Not really. You need to url encode the values because if they contain special characters the receiving page will not parse them correctly:
string url = "~/change.aspx?" +
"newemail=" + HttpUtility.UrlEncode(mail) +
"&newdate=" + HttpUtility.UrlEncode(date) +
"&newtime=" + HttpUtility.UrlEncode(time) +
"&newname=" + HttpUtility.UrlEncode(name);
Is it secure?
No, not at all. Anyone could send a request to your target page with whatever values he feels good for him.
Is there a better way?
That would depend on your specific requirements and whether the information you are transmitting is sensitive or not. If it is sensitive information, then you might consider storing the values on the server instead of passing them as query string parameters. For example you could use the ASP.NET Session for this purpose.
Is it secure? No, of course not, the values are on the query string which gets sent to the browser. If you want to keep it secure put the values in session on the server side.
You are using QueryString Way to pass variables from one page to another page.its not a problem if the parameters are not secure like you cant pass secure info(Sensitive Information) like passwords,any important ids...
if you want to handle with secure parameters(Sensitive Information) you can use Sessions,Cookies..
In your case you are passing names.i hope it doesnt create any problems because this are not secure parameters(Sensitive info).even though if you feel any security risks you can use encryption and decryption concepts like encrypt your parameter name and pass it with url and then decrypt that parameter where you want to use.
Refer :
http://msdn.microsoft.com/en-us/library/6c3yckfw%28v=vs.100%29.aspx
http://www.codeproject.com/Articles/8055/Transferring-page-values-to-another-page
For better understanding about passing variables from one page to another page
Thank you guys for you help!
I have now changed it to Sessions. My code now looks like this:
In web page1:
string email = txtEmail.Text;
string date = txtDate.Text;
string time = txtTime.Text;
string name = txtName.Text;
Session["email"] = email;
Session["date"] = date;
Session["time"] = time;
Session["name"] = name;
Response.Redirect("~/change.aspx");
In web page2:
protected void Page_Load(object sender, EventArgs e)
{
string email = (string)(Session["email"]);
string date = (string)(Session["date"]);
string time = (string)(Session["time"]);
string name = (string)(Session["name"]);
TextBox1.Text = email;
TextBox2.Text = date;
TextBox3.Text = time;
TextBox4.Text = name;
}
I use this way to rewite requested URL that have abc extension to aspx extension(found in SO):
void Application_BeginRequest(object sender, EventArgs e)
{
String fullOrigionalpath = Request.Url.ToString();
String[] sElements = fullOrigionalpath.Split('/');
String[] sFilePath = sElements[sElements.Length - 1].Split('.');
if (fullOrigionalpath.Contains(".abc") )
if (!string.IsNullOrEmpty(sFilePath[0].Trim()))
Context.RewritePath(sFilePath[0] + ".aspx");
}
but it seems this way is too slow. Can you say me How can I do this in web.config or other fast way?
Use Path.ChangeExtension. Don't Invent the wheel.
string aspxPath = Path.ChangeExtension(fullOrigionalpath, "aspx");
I am using Querystring to pass values from one page to other. I am tring to implement encoding and decoding using the Server.UrlDecode and urlEncode.
Query string returns a null value, but I can check the values are been sent in URL.
The two pages are:
QueryString.aspx
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string id = "1";
string name = "aaaa";
string url = string.Format("QueryStringValuesTransfer.aspx?{0}&{1}", Server.UrlEncode(id), Server.UrlEncode(name));
Response.Redirect(url);
}
;;
In another page :
QueryStringValuesTransfer.aspx:
public partial class QueryStringValuesTransfer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string id1 = Server.UrlDecode(Request.QueryString["id"]);
string name1 = Server.UrlDecode(Request.QueryString["name"]);
Response.Write(id1 + name1);
}
}
I am get null values in the id1 and name1.
Any help please..
Change this line:
string url = string.Format("QueryStringValuesTransfer.aspx?id={0}&name={1}", Server.UrlEncode(id), Server.UrlEncode(name));
Right now you are only setting the values in the querystring, you need to assign them names so you can grab them again:
string url = string.Format("QueryStringValuesTransfer.aspx?id={0}&name={1}", Server.UrlEncode(id), Server.UrlEncode(name));
That's because your query string should be something like
MyPage.aspx?id=xxx&name=yyy
You are not passing the values, only the names...
string url = string.Format("QueryStringValuesTransfer.aspx?{0}&{1}", Server.UrlEncode(id), Server.UrlEncode(name));
Should be:
string url = string.Format("QueryStringValuesTransfer.aspx?id={0}&name={1}", Server.UrlEncode(id), Server.UrlEncode(name));
You aren't specifying a name for the values. You need:
string url = string.Format("QueryStringValuesTransfer.aspx?id={0}&name={1}", Server.UrlEncode(id), Server.UrlEncode(name));
When constructing the URL in the first page you should do this:
string url = string.Format("QueryStringValuesTransfer.aspx?id={0}&name={1}", Server.UrlEncode(id), Server.UrlEncode(name));
The query string consists of key-value pairs, you should provide the keys.
I found a good way to check if a file exists and read the contents if it does, but for some reason I can't create a method out of it.
Here's what I have so far:
<script runat="server">
void Page_Load(Object s, EventArgs e) {
lblFunction.Text = mwbInclude("test.txt");
}
string mwbInclude(string fileName) {
string inc = Server.MapPath("/extra/include/" + Request["game"] + "/" + fileName);
string valinc;
if(System.IO.File.Exists(inc))
{
valinc = System.IO.File.ReadAllText(inc);
}
return valinc;
}
</script>
I wish I could provide more info, but the server this is on doesn't show any feedback on errors, just a 404 page.
I think
valinc = Response.Write(System.IO.File.ReadAllText(inc));
should be
valinc = System.IO.File.ReadAllText(inc);
Why are you setting the Text property and calling Response.Write? Do you want to render the text as a label, or as the whole response?
If you're getting a 404, it's because your page isn't being found, not because there's a problem with the script itself. Have you tried ripping out all of the code and just sticking in some HTML tags as a sanity check?