Get value off querystring by it's position - c#

I have a URL that's going to have one parameter on it but the 'name' of this parameter will, in some cases, be different eg.
www.mysite.com/blog/?name=craig
www.mysite.com/blog/?city=birmingham
and what I'm trying to do is always get the value (craig / birmingham) of the first (and only) parameter on the string regardless of it's name (name/ city). Is there any code that will do that or will I have to check the possible options?
thanks,
Craig

Try something like this:
string valueOfFirstQueryStringParameter = "";
string nameOfFirstQueryStringParameter = "";
NameValueCollection n = Request.QueryString;
if (n.HasKeys())
{
nameOfFirstQueryStringParameter = n.GetKey(0);
valueOfFirstQueryStringParameter = n.Get(0);
}

Related

How can I replace the parameters in my request url using C#?

I have n number of request urls, like below
https://{user.id}/{user.name}/testing1
https://{user.number}/{user.name}/testing1
https://{user.age}/{user.height}/testing1
https://{user.gender}/{user.name}/testing1
https://{user.height}/{user.age}/testing1
https://{user.weight}/{user.number}/testing1
I have the below test data class which has n number of values.
public class User{
public string id = "123";
public string name = "456";
public string age = "789";
public string gender = "1478";
public string height = "5454";
public string weight = "54547";
public string number = "88722";
.......
.......
.......
}
And I need to make the url
https://{user.number}/{user.name}/testing1 into
https://{88722}/{456}/testing1
In my code, I will be randomly getting a request url(from a json file) and i need to replace the parameters with the values given in the class. Can this be done? If yes please help. Thanks
I have tried using string.format() - but it doesnot work, because I am randomly getting the url and I am not sure which value needs to be replaced.
I also tried using the interpolation, but found not helpful either unless i can do something like
User user = new User();
string requesturl = GetRequestJSON(filepath);
//The requesurl variable will have a value like
//"https://{user.id}/{user.name}/testing1";
string afterreplacing = $+requesturl;
Update: After some more googling, I found out that my question is very much similar to this. I can use the first answer's alternate option 2 as a temporary solution.
Unless I am clearly missing the point (and lets assume all the values are in when you do new user(); )
You just want
User user = new User();
string requesturl = $"https://{user.id}/{user.name}/testing1";
then all the variables like {user.id} are replaced with values. such as "https://88722/456/testing1" if you need the {} in there you can add extra {{ to go with such as
string requesturl = $"https://{{{user.id}}}/{{{user.name}}}/testing1";

How to get onyl part of the URL in c#

I am creating a module using C# which will need to refer the URL
So i have 2 example URLs for you here.
http://www.website.com/ProductDetail/tabid/86/rvdsfpid/1gb-dual-port-iscsi-8660/rvdsfmfid/qlogic-174/Default.aspx
&&
http://www.website.com/ProductDetail/tabid/86/rvdsfpid/49950/default.aspx
Now what i need from both the URls is the product ID which in the first case is 8660 & the second case is 49950. I cannot change the way these URls are generated. The easiest way would have been
http://www.website.com/ProductDetail/tabid/86/default.aspx?rvdsfpid=49950
and then i could do the following and life would be easy.
string Pid= Request.Querystring["rvdsfpid"];
However since i dont have control on the way the URL is generatyed how can i catch the URL and fetch only the productId.
Assuming that's the URL format you're being passed and there's nothing else you can do....You're going to need to get the full url and then split it. Here's the basic, you're going to have to add some extra checks and stuff in there;
string url = "http://www.website.com/ProductDetail/tabid/86/rvdsfpid/1gb-dual-port-iscsi-8660/rvdsfmfid/qlogic-174/Default.aspx"
//string url = "http://www.website.com/ProductDetail/tabid/86/rvdsfpid/49950/default.aspx";
url = url.Replace("http://", ""); //get rid of that, add code to check for https?
string[] x = url.Split('/');
string productCode = x[5]; //assuming the product code is always the 6th item in the array!
string code = "";
if (productCode.IndexOf("-") > -1)
{
code = productCode.Substring(productCode.LastIndexOf("-")+1);
}
else
{
code = productCode;
}

why is my parameter value not being passed in QueryString

I have a parameter:
string custName = "";
custName = AddressDT.Rows[0]["first_name"].ToString() + " " + AddressDT.Rows[0]["last_name"].ToString();
I am performing my Response.Redirect
Response.Redirect("~/Account/EmailError.aspx?parm1=custName");
and I am retrieving the parameter value:
custName = Request.QueryString["parm1"];
when I run debug: ... custName = "custName"
what am I doing wrong? I have done this before with no issue.
Your Response.Redirect is passing the string "custName", not the value of the variable.
This will fix it for you:
Response.Redirect("~/Account/EmailError.aspx?param1=" + custName);
That is because you're using a String Constant as the value for the parameter.
Response.Redirect("~/Account/EmailError.aspx?parm1=custName");
That would always cause the URL to be set with the string custName.
Try using the variable name as:
Response.Redirect("~/Account/EmailError.aspx?parm1=" + custName);
Now the variable would be appended to the request.
Now when you'll run the code, it would produce the value in the Variable and you'll get the code running fine.
Always remember to finish the string and then adding the variable values. Everything inside the double qoutes is considered to be a string and not a variable name or a keyword.
If you already know that QueryString value is string, you need to use UrlEncode.
In addition, you want to use String.Format as much as possible for good design practice instead of +.
string custName = String.Format("{0} {1}",
AddressDT.Rows[0]["first_name"].ToString(),
AddressDT.Rows[0]["last_name"].ToString());
string url = String.Format("~/Account/EmailError.aspx?parm1={0}",
Server.UrlEncode(custName));
Response.Redirect(url);

Passing string to int to substring

I currently get the users number like this:
if (checkBox1.Checked)
{
rchars = numericUpDown1.Value.ToString();
}
else
{
rchars = "3";
}
string rchars; is a global variable.
So, I'm trying to remove the rchars from file names.
For example the first three characters from the file name:
int num = Int32.Parse(rchars);
foreach (FileInfo name in fpaths.GetFiles("*.mp3")
{
string snub = name.Name.Substring(num);
MessageBox.Show(snub);
System.IO.File.Move(blah + name.Name, newblah + snub);
}
My question is how can I get "num" to work in a substring? Since I can't get it to be a value. Since I want to pass it from the numericUpDown. Add make "num" a value so I can remove the value from the file names.
Thanks.
Value property of NumericUpDown is Decimal - that is perhaps why you are having issues. In your if block, I would consider casting the Value Property of NumericUpDown object into an integer in the true part and use integer value 3 in its else part. There after, I would avoid parsing again and give it to Substring as is.
You say you want to remove all occurrences of rchars from name, so why are you using Substring? If you want to remove the string rchars from name then just keep it as a string and use String.Replace:
string snub = name.Name.Replace( rchars, String.Empty );
Also, the Value property of a NumericUpDown is a decimal, not an int.
are you looking to replace the value "3" within the name of a file with something else/remove it? Like this:
original file:
string fiename = "myfile3.mp3";
remove/replace selected number (in this case 3):
string num = "3";
filename.replace(num, "");
should end up with a filename "myfile.mp3"

How to read dicom tag value using openDicom.net in C#?

I'm reading dicom tags using openDicom.net like this:
string tag = "";
string description = "";
string val_rep = "";
foreach (DataElement elementy in sq)
{
tag = elementy.Tag.ToString();
description = elementy.VR.Tag.GetDictionaryEntry().Description;
val_rep = elementy.VR.ToString();
}
How can I read dicom tag values?
the value.ToString() method isn't implemented. Implement your own method in Value.cs and you will get a value for "Value".
For example (only strings and numeric values):
public override string ToString()
{
return valueList[0].ToString();
}
I'm assuming that sq is a Sequence...
I've not worked with openDicom, but I'm pretty sure what you're doing there isn't going to yield the results you want.
You have a single tag, description, and val_rep variable, but you're filling them using a foreach, meaning the last DataElement in the Sequence will be the only values you retrieve. You would achieve the same effect by using:
string tag = sq[sq.Count - 1].Tag.ToString();
string description = sq[sq.Count -1].VR.Tag.GetDictionaryEntry().Description;
string val_rep = sq[sq.Count - 1].VR.ToString();
Thus retrieving the last set of values from the Sequence. I believe you'll find that if you step through the foreach as it executes, it will be loading all the different DataElements contained in your DICOM file.
Feel free to return a comment or post more information in your original post if I'm way off base here.
The tag value is retrieved as an array of generic object from the 'Value' member in 'DataElement' using the 'ToArray()' overriding method in the 'Value' class.
cniDicomTagList = new List<CNIDicomTag>();
foreach (DataElement element in sq)
{
string tag = element.Tag.ToString();
string description = element.VR.Tag.GetDictionaryEntry().Description;
object[] valueArr = element.Value.ToArray();
cniDicomTagList.Add(new CNIDicomTag(tag, description, valueArr));
}

Categories

Resources