【问题标题】:How to call one web api controller method into another web api project? [closed]如何将一个 web api 控制器方法调用到另一个 web api 项目中? [关闭]
【发布时间】:2020-04-19 06:33:31
【问题描述】:

我有两个不同的 asp.net core web api 项目 --> 项目 A 和项目 B。我想将项目 A 的控制器方法调用到项目 B 的控制器方法中。

例如,

方法 GetDepartments() 是 Project A 的控制器 的方法,它返回静态值。我想将此方法称为 Project B 的控制器中的另一个方法 GetStudents()

【问题讨论】:

标签: c# asp.net asp.net-core-webapi


【解决方案1】:

HttpGet Api 的以下代码示例 HttpClient:

            [HttpGet]
            public async Task<HttpResponseMessage> GetRequestAsync(HttpRequestMessage Query)
            {                   
                try
                {
                    using (HttpClient client = new HttpClient())
                    {                                                          
                        HttpResponseMessage response = await client.GetAsync("http://localhost:8080/document/quicksearch/"+ Query.RequestUri.Query);

                        if (response.IsSuccessStatusCode)
                        {
                            Console.Write("Success");
                        }
                        else
                        {
                            Console.Write("Failure");
                        }

                        return response;
                    }
                }
                catch (Exception e)
                {        
                    throw e;
                }
            }

HttpPost Api 的以下代码示例 HttpClient:

 Public async Task PostRequestAsync()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://localhost:6740");

            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("", "login")
            });

            var result = await client.PostAsync("/api/Membership/exists", content);

            string resultContent = await result.Content.ReadAsStringAsync();

            Console.WriteLine(resultContent);
        }
    }

【讨论】:

    【解决方案2】:

    您必须在 IIS 上托管 ProjectA,然后只需调用 API GetDepartments 从 托管 URL,来自项目 B 控制器中的 GetStudents()。

    public class ProjectBController : Controller  
    {  
        //your Hosted Base URL
        string Baseurl = "http://192.168.90.1:85/";      
        // GET: Student
        public async Task<ActionResult> GetStudents()  
        {  
            List<Student> StudentInfo = new List<Student>();  
    
            using (var client = new HttpClient())  
            {  
                //Passing service base url  
                client.BaseAddress = new Uri(Baseurl);  
    
                client.DefaultRequestHeaders.Clear();  
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
    
                //Sending request to find web api REST service resource GetDepartments using HttpClient  
                HttpResponseMessage Res = await client.GetAsync("api/ProjectA/GetDepartments");  
    
                //Checking the response is successful or not which is sent using HttpClient  
                if (Res.IsSuccessStatusCode)  
                {  
    
                    var ObjResponse = Res.Content.ReadAsStringAsync().Result;  
                    StudentInfo = JsonConvert.DeserializeObject<List<Student>>(ObjResponse);  
    
                }  
                //returning the student list to view  
                return View(StudentInfo);  
            }  
        }  
    }  
    
    public class Student
    {
       public int Id { get; set; }
       public string FirstName { get; set; }
       public string LastName { get; set; }
    }
    

    【讨论】:

    • 我已经在 iis 上托管,但我不确定如何实现您提到的第二部分。可以提供demo吗?
    【解决方案3】:

    您不必从另一个 API 控制器调用另一个项目的 API 控制器。相反,您应该将逻辑重构为一个公共类库并从两者中使用它。

    控制器旨在从 API 请求/响应管道中调用。

    【讨论】:

    • 你听说过微服务架构吗?
    • 问题是关于从另一个控制器调用一个控制器,而不是从另一个调用一个 Web API,这显然是可以做到的。
    • I have two different asp.net core web api projects --&gt; Project A and Project B ...
    猜你喜欢
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 2019-10-16
    • 2014-06-06
    相关资源
    最近更新 更多