访问Web API时获取404 Not Found

时间:2018-02-09 13:01:27

标签: asp.net rest asp.net-web-api xamarin.forms

在xamarin.forms中访问web Api时,我收到此错误,我无法解决

  

{StatusCode:404,ReasonPhrase:'Not Found',版本:1.1,内容:   System.Net.Http.StreamContent,Headers:{Server:Microsoft-IIS / 10.0   X-Powered-By:ASP.NET日期:星期五,2018年2月9日12:50:00 GMT   内容类型:text / html内容长度:1245}}

这是我的网络Api代码。

public IList<TCourse> GetData()
        {
            try
            {
                using (var context = new HCMSEntities())
                {
                    var cli = (from b in context.tblTrainingCourses
                               orderby b.CourseID
                               select new TCourse { CourseID = b.CourseID, Course = b.Course }
                               ).ToList();

                    return cli;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

这是消耗api的代码,我遇到了错误。

public async Task Index()
        {
            List<TCourse> EmpInfo = new List<TCourse>();

            try
            {
                using (var client = new HttpClient())
                {
                    //Passing service base url  
                    client.BaseAddress = new Uri("http://10.20.2.62/");

                    client.DefaultRequestHeaders.Clear();
                    //Define request data format  
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    //Sending request to find web api REST service resource GetAllEmployees using HttpClient  
                    HttpResponseMessage Res = await client.GetAsync("api/Course");

                    //Checking the response is successful or not which is sent using HttpClient  
                    if (Res.IsSuccessStatusCode)
                    {
                        //Storing the response details recieved from web api   
                        var EmpResponse = Res.Content.ReadAsStringAsync().Result;

                        //Deserializing the response recieved from web api and storing into the Employee list  
                        EmpInfo = JsonConvert.DeserializeObject<List<TCourse>>(EmpResponse);

                    }

                }

            }
            catch (Exception e)
            {

            }

        }

未部署web Api。我在基地址中尝试http://localhost/而不是切换到Ip。 不知道什么是错的,在哪里。 善意的指南。

0 个答案:

没有答案