Accessing the query string value using ASP.NET - asp.net

I have been trying to find the question to my answer but I'm unable to and finally I'm here. What I want to do is access the value passed to a webpage (GET, POST request) using asp.net. To be more clear, for example:
URL: http://www.foobar.com/SaleVoucher.aspx?sr=34
Using asp.net I want to get the sr value i.e 34.
I'm from the background of C# and new to ASP.NET and don't know much about ASP.NET.
Thanx.

Can you refer to this QueryString
Here he says how to access the query string using:
Request.Url.Query

That is not called a Header, but the Query String.
the object document.location.search will contain that and the javascript to get any query string value based on the key would be something like:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
code from other question: https://stackoverflow.com/a/901144/28004

Related

How to get string from formatted url in asp.net c#

Suppose We are on the page www.abc.com/apple-store
then how to get string apple-store in asp C# code.
to store into another variable.
You can use string.last() to extract it.
string lastPartUrl =HttpContext.Current.Request.Url.AbsoluteUri.Split('/').Last();
You should use the Request.RawUrl property. See more details here.
Alternatelly you can also use the Request.Url (see here) property to get different parts of the current URL. For example you will get the same result using Request.Url.LocalPath.
You can get the url in a string variable. Further you can implement the below logic which will save the value in a variable.
string str = "www.abc.com/apple-store";
string result = "";
int i= 0;
int len = str.Length;
//Get the index of the character
i = str.IndexOf('/');
//store the result in the variable
result = str.Substring(i+1,len-i-1);
Console.WriteLine("Resultant:- {0}", result);`
Hope this helps a bit.

Returning a value from a page

How is it possible to return a value in an ASP.NET web page? I need to return the value "1". I'm not sure how to do that. Is it as simple as writing 1 in the content of the .aspx file, or do I need to do anything else?
That's the request that is given:
GET /kartclient/kartlogin.aspx HTTP/1.1. Accept: text/*
Since kartlogin.aspx seems to be a user-login page, then are you interested in passing userID + password to another page?
Still not sure what you are trying to achieve, but if its only passing data from one page to another, there are many ways, here are some quick ones you can try:
On kartlogin.aspx:
1. Query String Method Send/Post Value:
string name="xyz";
Response.Redirect("Page2.aspx?name= "+name);
2. Cookie Method Send/Post Value:
HttpCookie myCookie = new HttpCookie("name");
myCookie.Value="xyz";
Response.Cookies.Add(myCookie);
3. Session Method Save Value:
Session["name"] = "xyz";
On Page2.aspx:
1. Query String Method Get Value:
string name = Response.QueryString.GetValue(" name ");
Response.Write(name);
2. Cookie Method Get Value:
string name = Request.Cookies('name');
Response.Write(name);
3. Session Method Get Value:
string name = Session["name"].ToString();
Response.Write(name);
You should take a look at:
How to: Pass Values Between ASP.NET Web Pages
Stackoverflow: How can I pass values from one form to another in Asp.net
Stackoverflow: When to use Request.Cookies over Response.Cookies?
Eight Different Ways to Transfer Data from One Page to Another Page

POST parameters in asp.net

I'm trying to get parameters received from a form, that were sent with method POST.
I don't know how it's called in asp, M$ loves to change stuff's names to mess with us. They come in HTTP body, while GET/QueryString parameters come in URL after the ? sign.
In PHP, "get patameters" are available in the $_GET array. In asp they are Request.QueryString["parameter1"].
"post patameters" are in $_POST, and I cant find it in asp. I hope I made it clear :p
To read the value from paramater1 contained inside the form data:
string paramater1 = Request.Form["paramater1"];
Note that if the form doesn't contain your variable, paramater1 will be null.
Suppose your querystring is something like this :
http://stackoverflow.com/questions.aspx?id=17844065&title=post-parameters-in-asp-net
if i am right then you are looking for this. Please note this is regarding ASP.Net, I have no idea about classic ASP. And this will not work on classic ASP, I believe.
You can use in cs,
if(Request["id"]!=null )
{
var id= Request["id"]; // gives you id as 17844065 string values
}
if(Request["title"]!=null )
{
var title= Request["title"]; // gives you title as string
}
Update :
NameValueCollection nvc = Request.Form;
string userName, password;
if (!string.IsNullOrEmpty(nvc["txtUserName"]))
{
userName = nvc["txtUserName"];
}
if (!string.IsNullOrEmpty(nvc["txtPassword"]))
{
password = nvc["txtPassword"];
}
Try Request.Params, it should contain all GET and/or POST parameters, Request.Form should contain only form parameters.

Retriving the url of a Hyperlink column which is in a sharepoint list, using Client object model

function IfModuleSucceded(sender, args) {
var existingCount = existingItems.get_count();
var existEnumerator = existingItems.getEnumerator();
while (existEnumerator.moveNext()) {
var currentmodule = existEnumerator.get_current();
var URL = currentmodule.get_item("Request_URL");
alert(URL);
}
}
In this Code i am trying to Retrieve the url of a Hyperlink column which is in a SharePoint list, using Client object model, but i have received an object. How could i get the Url out of this received object ????
when this code is executed, it gives the alert as "[Object Object]".
would anyone help me to sort this out ??
The answer will be alert(url.url) as it's an object.
It will also have a property called description
The Hyperlink field has two properties: Description and Url.
You can access the properties like this: ObjectName.PropertyName
So for your URL object in your example, you can reach the properties like this: URL.Url and URL.Description
I found that Url and Description are case sensitive, so make sure you capitalize where necessary.
This worked great for me.

ASP.NET & Ajax: query string parameters using ISO-8859-1 encoding

Here's another one for you to help me solve: I have an ASP.NET website that uses AJAX (asynchronous) calls to am .ashx handler, passing a query string parameter to get some information from the database.
Here's an example of how it works:
Client-side (Javascript) code snippet that makes the asynchronous call to the handler:
/* Capture selected value from a DropDownBox */
var dropdown = document.getElementById(DropDownID);
var selectedValue = dropdown.options[dropdown.selectedIndex].value;
/* Make the call to the handler */
var url = "MyHandler.ashx?param=" + selectedValue;
var ajaxObj = new Ajax();
ajaxObj.doRequest(url, MyCallback, args, connectionFailed);
When I load the webform (that contains this AJAX call) for the first time, it sends the right query string to the handler (I checked it using debug in Visual Studio), like param = Street Joseph Blíss. That's the right behavior I want it to have.
The thing is that when I load that webform again (and all subsequent times), that í character from "Blíss" appears in server-side as í-. As that's the key from the entity I'm trying to select on server-side database access script, it doesn't work as it worked on 1st webform load.
I tried encoding the query string on client-side and decoding it on server-side, using something like this:
Client-side (Javascript):
var encodedParam = encodeURIComponent(selectedValue);
/* Make the call to the handler */
var url = "MyHandler.ashx?param=" + encodedParam ;
Server-side (ASP.NET, C#):
string encodedParam = context.Request.QueryString["param"];
string value = HttpUtility.UrlDecode(encodedParam, Encoding.ASCII);
...but I had no luck with it and the problem still remains. Any help?
After some more searching, I found out how to solve with server-side code refinement. Here's the deal:
I had to alter my .ashx handler to parse the original parameter grabbed from the query string and convert it into UTF-8. Here's how it's made:
// original parameterized value with invalid characters
string paramQs = context.Request.QueryString["param"];
// correct parsed value from query string parameter
string param = Encoding.UTF8.GetString(Encoding.GetEncoding("iso8859-1").GetBytes(paramQs));
Happy coding, folks! :)

Resources