-2

I am trying to store my web address in a variable, but I keep getting an error of

Cannot implicitly convert type 'System.Collections.Specialized.NameValueCollection' to 'string'

ANd this is my syntax

protected void Page_Load(object sender, EventArgs e)
{
    GetKeyInfo();
}

protected void GetKeyInfo()
{
    //The below line is the problem line
    string fulladdress = HttpContext.Current.Request.Url.Query;
    Response.Write(fulladdress);
    string parsed = System.Web.HttpUtility.ParseQueryString(fulladdress);
}

EDIT
This is the address shown in browswer where I am attempting to run this http://www.internaltesting.com/PassingDataTest/default.aspx?Information=https://XXX.XXX.X.XXX/all?token=1&cid=1832&employeeid=16432

And this is full error that is given

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

Source Error:
Line 29: string fulladdress = HttpContext.Current.Request.Url.Query;
Line 30: Response.Write(fulladdress);
Line 31: var parsed = System.Web.HttpUtility.ParseQueryString(fulladdress);
Line 32: string token = parsed["token"];
Line 33: string employeeid = parsed["employeeid"];

EDIT
Is this the line from web.config you are after

<compilation debug="true">
  <assemblies>
    <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </assemblies>
</compilation>
FunFly WhiteGuy
  • 163
  • 2
  • 2
  • 11
  • 4
    the error is telling you what the problem is.. you are trying to assign a List to a string what if you change string to `var parsed` it will compile or change your code to `NameValueCollection parsed = System.Web.HttpUtility.ParseQueryString("myAddress");` and add to the using at the top of your class the following `using System.Collections.Specialized;` does google work in your neck of the woods..? look at the msdn documentation -https://msdn.microsoft.com/en-us/library/ms150046(v=vs.110).aspx – MethodMan Feb 09 '16 at 20:48
  • @MethodMan - var throws an error also – FunFly WhiteGuy Feb 09 '16 at 21:00
  • 1
    no it does not.. I tested it and it compiles.. please paste an example of what fulladdress is when you debug the code.. – MethodMan Feb 09 '16 at 21:01
  • you probably are not even using the debugger and secondly the value of `fullAddress` is probably `string.Empty` – MethodMan Feb 09 '16 at 21:05
  • 1
    @FunFlyWhiteGuy what version of C#/.NET are you using? The support for `var` has came out a long time ago. – DPac Feb 09 '16 at 21:09
  • 2
    Do you have a part in your `web.config` which looks like ``? – Mark Feb 09 '16 at 21:13
  • 2
    also read the following this should be something that you should be doing on your end {MSDN Implicitly Typed Local Variables](https://msdn.microsoft.com/en-us/library/bb384061.aspx) I also told you that if you didn't want to use the word `var` that you would need to declare your variable as the following `NameValueCollection parsed = System.Web.HttpUtility.ParseQueryString("myAddress");` and add to the top of the .cs file `using System.Collections.Specialized` – MethodMan Feb 09 '16 at 21:15
  • also right click on the project and click Properties and tell us what .net version you are coding this in.. – MethodMan Feb 09 '16 at 21:20

1 Answers1

2

System.Web.HttpUtility.ParseQueryString returns a NameValueCollection containing all the query string parameters, it's not a single string.

var parsed = System.Web.HttpUtility.ParseQueryString(fulladdress);
string singleParam = parsed.Get("OneParam");

or for ancient .NET/C# version that don't support var:

System.Collections.Specialized.NameValueCollection parsed = System.Web.HttpUtility.ParseQueryString(fulladdress);
string singleParam = parsed.Get("OneParam");

See also: https://msdn.microsoft.com/en-us/library/ms150046.aspx (HttpUtility.ParseQueryString Method) and How to read the query string params of a ASP.NET raw URL?

Community
  • 1
  • 1
Mark
  • 1,360
  • 1
  • 8
  • 14