Getting to the query string (GET request array) inside a web service in .NET

I’m looking to find a way to access the .net query string contained in the standard ASP.NET request object inside a web service. In other words if I set a SOAP web service to this url:

http://localhost/service.asmx?id=2

Can I access the ID Get variable?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I just looked for “Request” of the context in asmx file and I saw that. But I’m not sure if it is right.

this.Context.Request.QueryString["id"];

Method 2

HttpContext.Current.Request.QueryString[“id”]

Method 3

Since you ask, I guess there is no HttpContext.Current.Request ?

Method 4

While searching for the solution of the same problem i decided to take different approach.
My query string was packed with lots of variables and since I was not able to access query string data from the web service, and I also did not want to send each query string variable as a separate parameter, I prepared my web method to expect one aditional string parameter.

That parameter was window.location (entire url of the page) in my javascript function on .aspx page

Once I had url in my web service, the rest was quite stright forward

Uri myRef = new Uri(stringMyWindowLocationParameter);
System.Collections.Specialized.NameValueCollection mojQuery = HttpUtility.ParseQueryString(myRef.Query);

Now my query string is contained inside myRef object and this is how I call it

// Instead trying to request query string like this
string myId = HttpContext.Current.Request.QueryString["id"];

// ... I called it like this
string myId = myRef["id"];

Maybe it’s not the most elegant way but it solved my problem.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x