This is my url which contains 4 querystrings(desc,url,img,title).
http://localhost:4385/Default?desc=Home%20Page&url=http://localhost:4385/&img=http://localhost:4385/images/ribbon-img.png&title=
I read querystrings like below,
string title = Request.QueryString["desc"];
string pageurl = Request.QueryString["url"];
string alttext = Request.QueryString["title"];
string imageurl = Request.QueryString["img"];
The output i get is:
title=Home Page&url=http://localhost:4385/&img=http://localhost:4385/images/ribbon-img.png&title="
it takes entire url to first querstring, this is not my expected output.
I expect values to all querystring variables
can anyone please help me
The URL format is incorrect i feel, because the slash / character will be sent as %2F in the query string but that was not done in your URL format.
Update:
Respose.Redirect("http://localhost:4385/Default?desc=Home%20Page&url="+Uri.EscapeDataString("http://localhost:4385/")+"&img="+Uri.EscapeDataString("http://localhost:4385/images/ribbon-img.png")+"&title=");
The problem is that you are not creating the QueryString with proper encoding. .NET framework has HttpUtility.ParseQueryString Method to simplify this problem of encoding. Try this code
//are you sure your URL doesn't have an ".aspx" extension?
var url = " http://localhost:4385/Default.aspx?";
var queryString = System.Web.HttpUtility.ParseQueryString(string.Empty);
queryString["desc"] = "Home Page";
queryString["url"] = "http://localhost:4385/";
queryString["image"] = "http://localhost:4385/images/ribbon-img.png";
queryString["title"] = "";
Response.Redirect(url + queryString.ToString());
Now the QueryString will look like this.
var urlWithQueryString = " http://localhost:4385/Default.aspx?desc=Home+Page&url=http%3a%2f%2flocalhost%3a4385%2f&image=http%3a%2f%2flocalhost%3a4385%2fimages%2fribbon-img.png&title="
Now parsing can be done using the method you tried
string title = Request.QueryString["desc"];
string pageurl = Request.QueryString["url"];
string alttext = Request.QueryString["title"];
string imageurl = Request.QueryString["image"]; //you have wrongly typed "img" here
Related
I have a string with me and I want to call a rest method from that string. Can anyone think of a better approach then the one I described below for converting str to Rest Call Url.
str : https://P1.P2.com/ProjectName/_build?definitionId=123
Extract P1 and ProjectName and definitionId
Rest Call : https://dev.azure.com/P1/ProjectName/_apis/pipelines/123/runs?api-version=6.0-preview.1
Here, after getting the string, split it with "//" and then split array[1] with "/" and then get P1, ProjectName and then find something with "=" and then the number ?
Not sure if this might be ok.
Update :
I can get this as a URI. But how to convert it then ?
First off, use Uri to parse out the host, path, and query parts of the URI:
var uri = new Uri("https://P1.P2.com/ProjectName/_build?definitionId=123");
For extracting P1 from the host, it's probably going to be easiest to split on .:
string subdomain = uri.Host.Split('.')[0]
For ProjectName, just look at uri.Segments:
string projectName = uri.Segments[1];
For definitionId, parse the query string with HttpUtility.ParseQueryString:
var query = HttpUtility.ParseQueryString(uri.Query);
string definitionId = query["definitionId"];
To construct the new Uri, use a UriBuilder:
var builder = new UriBuilder()
{
Scheme = "https",
Host = "dev.azure.com",
Path = $"{subdomain}/{projectName}_apis/pipelines/{definitionId}/runs",
Query = "api-version=6.0-preview.1",
};
string result = builder.ToString();
See it on dotnetfiddle.net
I have two string:
string url = HttpContext.Current.Request.Url.AbsoluteUri;
//give me :
//url = http://localhost:1302/TESTERS/Default6.aspx?tabindex=2&tabid=15
And:
string path = HttpContext.Current.Request.Url.AbsolutePath;
//give me:
//path = /TESTERS/Default6.aspx
Now I want to get the string:
http://localhost:1302
So what I am thinking of is I will find the position of path in url and remove the sub-string from this position in url.
What I tried:
string strApp = url.Remove(url.First(path));
or
string strApp = url.Remove(url.find_first_of(path));
but I can't find the write way to express this idea. How can I archive my goal?
So basically you want the URL, from the start up to the beginning of your path.
You don't need to "remove" that part, only take characters up to that precise point. First, you can get that location with a simple IndexOf as it returns the position of the first character that matches your string. After this, simply take the part of url that goes from 0 to that index with Substring.
string url = "http://localhost:1302/TESTERS/Default6.aspx?tabindex=2&tabid=15";
string path = "/TESTERS/Default6.aspx";
int indexOfPath = url.IndexOf(path);
string strApp = url.Substring(0, indexOfPath); // gives http://localhost:1302
Which you can shorten to
string strApp = url.Substring(0, url.IndexOf(path));
you can also do something like below code to get the Host of URI
Uri uri =HttpContext.Current.Request.Url.AbsoluteUri ;
string host = uri.Authority; // "example.com"
Here is another option.. this doesn't require any string manipulation:
new Uri(HttpContext.Current.Request.Url, "/").AbsoluteUri
It generates a new Uri which is the path "/" relative to the original Url
You should just use this instead:
string baseURL = HttpContext.Current.Context.Request.Url.Scheme + "://" +
HttpContext.Current.Context.Request.Url.Authority;
This should not be solved using string manipulation. HttpContext.Current.Request.Url returns an Uri object which has capabilities to return the information you request.
var requestUrl = HttpContext.Current.Request.Url;
var result = requestUrl.GetComponents(UriComponents.SchemeAndServer,
UriFormat.Unescaped);
// result = "http://localhost:1302"
I want to use a console application to easily divide a URL from its params so I can URL encode the parameters. Is there a type that makes this simple? Please keep in mind that this is being run outside of a web server.
The URL is of the form
E.G.
string url = "http://sitename/folder1/someweb.dll?RetrieveTestByDateTime?PatientID=1234¶m2=blah blah blah";
So then I can do
string finalUrl = "http://sitename/folder1/someweb.dll?RetriveTestByDateTime?PatientID=" + HttpUtility.UrlEncode(PatientIDValue) + HttpUtility.UrlEncode(param2Value);
Second, is the second ? valid? This data comes from a 3rd party app. Surely this can be accomplished with Regex, but I prefer not to use it as most people on my team do not know regular expressions.
Use the Uri class - pass in the string url to the constructor and it will parse out the different parts.
The query will be in the Query property and you can reconstruct the URL easily using the Scheme, AbsolutePath and the encoded Query.
Firstly your url structure is bad
string url = "http://sitename/folder1/someweb.dll?RetrieveTestByDateTime?PatientID=1234¶m2=blah blah blah";
should be
string url = "http://sitename/folder1/someweb.dll?RetrieveTestByDateTime&PatientID=1234¶m2=blah blah blah";
A possible solution, I'm not in a development environment now so this is from the head. Some changes may need to be done to the code.
string url = "http://sitename/folder1/someweb.dll?RetrieveTestByDateTime&PatientID=1234¶m2=blah blah blah";
var uri = new Uri(url);
var queryString = uri.Query;
NameValueCollection query = HttpUtility.ParseQueryString(queryString)
query["PatientID"] = string.Concat(HttpUtility.UrlEncode(PatientIDValue), HttpUtility.UrlEncode(param2Value));
// Rebuild the querystring
queryString = "?" +
string.Join("&",
Array.ConvertAll(query.AllKeys,
key => string.Format("{0}={1}",
HttpUtility.UrlEncode(key),
HttpUtility.UrlEncode(query[key]))));
Is there any easy way to convert email address string into proper URI format?
Input:
http://mywebsite.com/validate_email/3DE4ED727750215D957F8A1E4B117C38E7250C33/myemail#yahoo.com
Output should be:
http://mywebsite.com/validate_email/3DE4ED727750215D957F8A1E4B117C38E7250C33/myemail%40yahoo.com
If I didn't do the output, I get an error response like
An Error Was Encountered
The URI you submitted has disallowed characters.
Thanks in advance !
Description
You have to extract your QueryString from the url, encode them and build the new url.
Sample
string url = "http://mywebsite.com/validate_email/3DE4ED727750215D957F8A1E4B117C38E7250C33/myemail#yahoo.com";
int index = url.LastIndexOf("/");
string queryString = url.Substring(index + 1, url.Length - (index + 1));
url = url.Substring(0, index) + "/" + HttpUtility.UrlEncode(queryString);
// url is now
// http://mywebsite.com/validate_email/3DE4ED727750215D957F8A1E4B117C38E7250C33/myemail%40yahoo.com
More Information
String.LastIndexOf Method
String.Substring Method
HttpUtility.UrlEncode Method
This seems like a really easy one but everything I try doesn't seem to work
say I have the following string:
string myString = "http://www.mysite.com/folder/file.jpg";
How can I process that to remove the URL and just leave "file.jpg" as the string value?
Thanks!
Kris
You can always use System.IO.Path methods
string myString = "http://www.mysite.com/folder/file.jpg";
string fileName = Path.GetFileName(myString); // file.jpg
If you do want to process more complex URIs you can pass it thought the System.Uri type and grab the AbsolutePath
string myString = "http://www.mysite.com/folder/file.jpg?test=1";
Uri uri = new Uri(myString);
string file = Path.GetFileName(uri.AbsolutePath);
string lastPart = myString.Substring(myString.LastIndexOf('/') + 1);