.net MVC接口编写和调用

1、引用:

using System.Web.Mvc;
using System.Net.Http;
using System.Net.Http.Headers;

2、后台接口:

        /// 
        /// 根据手机号码与用户信息获得相关的房屋信息
        /// 
        /// 
        /// 
        [HttpPost]
        public List QueryHouseCentersByOwnerMember(OwnerMemberView model)
        {
            var repairs = WYGLService.OwnerMembers.GetHouseCentersByOwnerMember(model);
            return repairs;
        }

3、后台调用:

        /// 
        /// 根据手机号码与用户信息获得相关的房屋信息
        /// 
        /// 
        /// 
        public string QueryHouseCentersByOwnerMember()
        {
            string address = "http://localhost/wygl/api/";
            HttpClientHandler handler = new HttpClientHandler();
            HttpClient httpClient = new HttpClient(handler);
            httpClient.BaseAddress = new Uri(address);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            string action = "CSComplaint/QueryHouseCentersByOwnerMember";
            string msg = "";
            var company = RepositoryFactory.EntityRepositoryService.Companies.GetFirst(1);
            HttpResponseMessage respose = httpClient.PostAsJsonAsync(action, new OwnerMemberView { ContactPhone = "00000000", CompanyID = 1, ProjectID = 1 }).Result;
            if (respose.IsSuccessStatusCode)
            {
                var result = respose.Content.ReadAsAsync<List<HouseCenterMember>>().Result;
            }
            else
            {
                msg = respose.StatusCode.ToString();
            }

            return msg;
        }

4、总结:
这个方法简单易懂。

你可能感兴趣的:(.net随笔)