【问题标题】:How to use an api's URL inside an asp.net core controller如何在 asp.net 核心控制器中使用 api URL
【发布时间】:2021-06-09 11:22:34
【问题描述】:

所以我需要创建一个使用以下 URL api 发送短信的 C# API SMSController

https://[WEBSITELINK]/bestsmsbulkapi/sendSms.php?username=USERNAME&&password=PASSWORD&&message=MESSAGE&&senderid=SENDERID&&destination=DESTINATION

我该怎么做?

【问题讨论】:

  • 您需要在此处提供更多详细信息。我将首先查看您的端点需要什么来满足请求。是GETPOST 吗?在你这样做之后,我建议查看HttpClient 来发送该请求。更具体地说,如果您使用.net core,我建议使用IHttpClientFactory 接口实现HttpClient。在此处查看更多信息docs.microsoft.com/en-us/aspnet/core/fundamentals/…

标签: c# asp.net api asp.net-core url


【解决方案1】:

你没有提到你的 api 方法接受 get 或 post 方法.. 以下代码用于获取方法,这可能对您有所帮助。

using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:60464/api/");
                //HTTP GET
                var responseTask = client.GetAsync("student");
                responseTask.Wait();

                var result = responseTask.Result;
                if (result.IsSuccessStatusCode)
                {

                    var readTask = result.Content.ReadAsAsync<Student[]>();
                    readTask.Wait();

                    var students = readTask.Result;

                    foreach (var student in students)
                    {
                        Console.WriteLine(student.Name);
                    }
                }
            }
            Console.ReadLine();

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

【讨论】:

    猜你喜欢
    • 2021-03-31
    • 2019-09-27
    • 2021-02-16
    • 2018-03-30
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    相关资源
    最近更新 更多