Share via


How to call Http.GetStringAsync with parameter?

Question

Tuesday, July 2, 2019 11:38 AM

Hello!

So, I'm trying out Blazor and can't figure out how to post a parameter. I'm new to C# so the question is probably not Blazor-unique. See code below:

Calling code in razor page:

       string strPropString;
       protected override async Task OnInitAsync()
        {
            strPropString = await Http.GetStringAsync("api/Data/lookupId/");
        }

the response is generated from the below code:   

        [HttpGet("[action]")]
        public string lookupId()
        {
            string connectionString = "connstring;";
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                con.Open();
                using (SqlCommand myCmd = new SqlCommand("sp_select_returnPropBetFromId", con))
                {
                    myCmd.CommandType = CommandType.StoredProcedure;
                    myCmd.Parameters.Add(new SqlParameter("@propId", 1));
                    using (SqlDataReader reader = myCmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                return reader.GetValue(0).ToString();
                            }
                        }
                    }
                }
            }
        }

Above line in bold just uses the static sql parameter of "1". How can I pass a parameter from the code that makes the http call?

Cheers!

/Eskil

All replies (4)

Tuesday, July 2, 2019 2:49 PM ✅Answered

Hello,

See the following post for how to pass parameters.

https://stackoverflow.com/questions/36280947/how-to-pass-multiple-parameters-to-a-get-method-in-asp-net-core

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow


Wednesday, July 3, 2019 4:46 AM ✅Answered

Maybe you should do something like this:

 

strPropString = await Http.GetStringAsync("api/Data/lookupId*?id=1**");*

* *

. . .

* *

[HttpGet("[action]")]

public string lookupId( string id )

{

*   . . .*

*   myCmd.Parameters.Add(new SqlParameter("@propId", id));*

*   . . .*

}

 

By the way, there are special forums for Blazor, etc.:

 

 


Wednesday, July 3, 2019 2:09 AM

Hi Eskilote,

Thank you for posting here.

Based on your description, you want to know how to use Http.GetStringAsync method. I suggest that you could refer to the following link.

http://www.herongyang.com/C-Sharp/Async-GetStringAsync-Method-Example-Program.html

>>How can I pass a parameter from the code that makes the http call

I am sure what you mean. what is your goal of doing this? I want to know if you want to do the thing in the following link.

https://stackoverflow.com/questions/43003569/passing-parameter-string-to-getasync

**Note:**This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

Best Regards,

Jack

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Wednesday, July 3, 2019 9:48 AM

That was straightforward, thanks for the answers!

Also, thanks for the tip on Blazor related forum.