Please help me to get query string value from the URL.
http://test.com/test.aspx#id=test
I tried to access with
Request.QueryString["id"]
Its getting null value.Please suggest how to access id from the url.
Thanks
I agree with everyone that the # should be a ?, but just FYI:
Note it isn't actually possible get the anchor off the URL, for example:
http://test.com/test.aspx#id=test
The problem is that # specified an anchor in the page, so the browser sees:
http://test.com/test.aspx
And then looks in the page for
<a id="test">Your anchor</a>
As this is client side you need to escape the # from the URL - you can't get it on the server because the browser's already stripped it off.
If you want the part after the # you have to copy it using Javascript before the request is sent to the server, and put the value in the querystring.
More info here c# get complete URL with "#"
A query string starts with a question mark ? not a hash #.
Try:
http://test.com/test.aspx?id=test
Using a hash, you're asking to jump to a named anchor within the document, not providing a query string
Your URL is not valid.
http://test.com/test.aspx#id=test
refers to a bookmark named id=test.
You should use
http://test.com/test.aspx?id=test
And then Request.QueryString["id"] will work.
If you would like to use it as hash tag you can use:
string value = Request.Url.ToString().Split('#')[1];
with this code, you will have your hash tag value.
Isnt it supposed to be?
http://test.com/test.aspx?id=test
Related
Problem - The query string parameter value is getting truncated if I have # in the parameter string value.
I have a table where I am binding the anchor tag <a/> prop href value like below;
<a class="btn btn-primary" href="/FileDetails/DownloadBlob?dirName=#data.FolderName&blobName=#data.FileName">
after binding on browser:
<a class="btn btn-primary" href="/FileDetails/DownloadBlob?dirName=RSHSYNCADE&blobName=J231132 anyfoostringnjectable barfoorBstringch #212422627, string Batch #145876V.pdf">
In the above anchor tag for href prop value I am setting a Controller/Action along with the query string parameters dirName and blobName.
But when the control comes to the specified action method of the controller it truncates the second param value from # means I can only see a value upto J231132 anyfoostringnjectable barfoorBstringch.
While trying to find a fix on internet, I am unable to find a proper solution which fit to my scenario till now.
Can anyone please help me to understand what causing this issue and what would be the fix for these kind of issues?
You need to encode the url that you put in the anchor href. If that values comes from the server, use HttpServerUtility.UrlEncode when you set the href tag. Otherwise you can use encodeURIComponent function in javscript or replace the "#" with "%23"
Query string URL must be encoded, there is some special characters for instance if you put plus(+) in query string. It will be handle as space because handler
will detect it as an encoded string then will replace it with space.
Encoded form of plus(+) is %2B
Encoded form of sharp(#) is %23
if you send the sharp value as %23 will handle as a sharp(#) and you won't have problem.
As helper you may use URi builders. There is many NuGet package exist.
in asp.net core view,you could try with asp-route-yourkeyor asp-all-route-data
I tried as below :
<a asp-action="AnotherAction"asp-route-atm="999#999">somelink</a>
Result:
I have a webapp created using C# and asp.net. I placed a parameter value in the querystring with a plus(+) sign. But the plus sign disappear.
How can I include the plus sign(+) in the query string without disappearing?
Here I found the same question and according to it, I have used Server.UrlEncode(myqerystring) and the time of decoding Server.UrlDecode(myqerystring) but some how it always resolves to the SPACE here is watch window
1) Querystring after the Server.UrlEncode()
2) Querystring after the Server.UrlDecode()
notice the space between S and R it should be +. I am new to all web development and I read other answers which says use UrlEncode and decode but it giving the same issue as before am I doing something wrong and yes the query string is automatically generated. I have no control over it.
There is other hack replace the " " or "%2b" with "+" I will go to that if I dont find any good way. So is there any good way to do this. Thanks.
The answer you link to just mentions using Server.UrlEncode, not Server.UrlDecode. When you read from Request.Querystring it automatically decodes the string for you. Doing it manually a second time is corrupting it and is why you're getting a space.
You can take a look at http://msdn.microsoft.com/en-us/library/zttxte6w(v=vs.110).aspx
Although this might help
string destinationURL = "http://www.contoso.com/default.aspx?user=test";
NextPage.NavigateUrl = "~/Finish?url=" + Server.UrlEncode(destinationURL);
Regarding plus sign you can not do this as '+' sign has semantic meaning in query string
Take a look at Plus sign in query string
EDIT
Have you used '+' sign while using google search. This provide different results.
Does anyone know how to get the entire string?
Example:
var result = Request.QueryString[id];
returns "Jack" instead of "Jack & Jill" for the URL "http://website.com/test.html?=Jack&Jill
The problem is not in reading the parameter, but in constructing it. You have to change your link, or the code that creates the link.
You have to use URL escaping encoding:
http://website.com/test.html?=Jack%26Jill
URL encoding is supported in .NET (HttpUtility) and JS (global functions) as well.
& is a special character used to seperate paramaeters being passed. You need to encode your Url using ASP.NET provided functions.
Please use HttpServerUtility.UrlEncode() when assigning the url to id
I'm trying to pull in an src value from an XML document, and in one that I'm testing it with, the src is:
<content src="content/Orwell - 1984 - 0451524934_split_2.html#calibre_chapter_2"/>
That creates a problem when trying to open the file. I'm not sure what that #(stuff) suffix is called, so I had no luck searching for an answer. I'd just like a simple way to remove it if possible. I suppose I could write a function to search for a # and remove anything after, but that would break if the filename contained a # symbol (or can a file even have that symbol?)
Thanks!
If you had the src in a string you could use
srcstring.Substring(0,srcstring.LastIndexOf("#"));
Which would return the src without the #. If the values you are retreiving are all web urls then this should work, the # is a bookmark in a url that takes you to a specific part of the page.
You should be OK assuming that URLs won't contain a "#"
The character "#" is unsafe and should
always be encoded because it is used in World Wide Web and in other
systems to delimit a URL from a fragment/anchor identifier that might
follow it.
Source (search for "#" or "unsafe").
Therefore just use String.Split() with the "#" as the split character. This should give you 2 parts. In the highly unlikely event it gives more, just discard the last one and rejoin the remainder.
From Wikipedia:
# is used in a URL of a webpage or other resource to introduce a "fragment identifier" – an id which defines a position within that resource. For example, in the URL http://en.wikipedia.org/wiki/Number_sign#Other_uses the portion after the # (Other_uses) is the fragment identifier, in this case indicating that the display should be moved to show the tag marked by ... in the HTML
It's not safe to remove de anchor of the url. What I mean is that ajax like sites make use of the anchor to keep track of the context. For example gmail. If you go to http://www.gmail.com/#inbox, you go directly to your inbox, but if you go to http://www.gmail.com/#all, you'll go to all your mail.
The server can give a different response based on the anchor, even if the response is a file.
When I run my project I get the url http://localhost:5973/PageToPageValuePass/Default.aspx I want to Encode the URL since sometimes I need to transfer data from page to page. When the urls are encoded then it increases the reliability.
Server.UrlEncode("http://www.google.com/c#");
I get this, but how do I use it to help me encode the url?
If your encoding parts of the path:
System.Uri.EscapeUriString("c#")
If your encoding 'arguments':
String.Format( "http://something/?test={0}", System.Uri.EscapeDataString("c#") );
try this
in ASP.NET
Server.UrlEncode("http://www.google.com/c#");
in WinForms using System.Web.dll
HttpUtility.UrlEncode("http://www.google.com/c#");
Url encoding is used to ensure that special symbols included in a url (most likely in a querystring) are not mistakenly interpreted as those used in the parsing and processing of a url. For example, the + symbol is used to indicate a space in a url. However, if you were intending for a + symbol to be a part of your querystring then you would want to encode that querystring before sending it to a browser.
For example. Imagine you have written a page that receives a math equation on the querystring and displays that equation on the page.
The url might be: http://yoursite.com/displayMath.aspx?equation=3+5
The + symbol in this case is intended to be a meaningful part of the equation. However, without a UrlEncode it would be interpreted as representing a space. Reading this value from the querystring on the receiving page would yield "3 5", which is not what was intended.
Instead of redirecting to that url directly, you would want to URL encode the request first. You might write the following code:
string equation = "3+5";
string url = String.Format(#"http://yoursite.com/displayMath.aspx?equation={0}", equation);
string encodedUrl = Server.UrlEncode(url);
Response.Redirect(encodedUrl);
This would ensure that a subsequent Request.Querystring["equation"] would receive the equation intact because any special symbols would first be encoded.
I'm not sure I understand your use case for encoding urls. If you could perhaps provide more information on what you are trying to achieve I will attempt to answer more fully. For now I hope that this information is useful.
say you want to create a link with some parameters you can use it as follows:
aspx:
Click Here
code behind:
myLink.Href = Page.ResolveClientUrl("~/MyPage.aspx") + "?id=" +
Server.UrlEncode("put here what ever you want to url encode");
Or as in your question:
myLink.Href = "http://www.google.com/")+Server.UrlEncode("C#");
this will put in html:
<a id="myLink" runat="server" target="_self" href="http://www.google.com/c+c%23">