public BaseResponse <ZipCodeResponseObject> GetAdressFromZipCode([FromBody] BaseRequest <ZipCodeRequestObject> request) { BaseResponse <ZipCodeResponseObject> response; ZipCodeResponseObject adress = new ZipCodeResponseObject(); string zipcode = request.RequestObject.ZipCode.ToString(); try { string patern = @"\d{5}"; Regex r = new Regex(patern); string fileName = HostingEnvironment.MapPath(@"~/App_Data/sampleadress.txt"); StringBuilder sb = new StringBuilder(); const Int32 BufferSize = 128; using (var fileStream = File.OpenRead(fileName)) using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize)) { String line; while ((line = streamReader.ReadLine()) != null) { if (line.Contains(zipcode)) { sb.Append(line + "<br/>"); } } } adress.Adress = sb.ToString(); response = new BaseResponse <ZipCodeResponseObject>(adress); } catch (Exception ex) { response = new BaseResponse <ZipCodeResponseObject>(new ZipCodeResponseObject()); response.Header.ResponseCode = "-1"; response.Header.ResponseMessage = ex.Message; response.Header.ResponseDetailMessage = ex.InnerException == null ? string.Empty : ex.InnerException.ToString(); response.Header.ResponseStatus = false; } return(response); }
public ActionResult GetAdress(string zipcode) { ZipCodeRequestObject obj = new ZipCodeRequestObject { ZipCode = zipcode }; BaseRequest <ZipCodeRequestObject> request = new BaseRequest <ZipCodeRequestObject>(obj); HttpClient client = new HttpClient() { BaseAddress = new Uri(address) }; HttpResponseMessage response = client.PostAsJsonAsync <BaseRequest <ZipCodeRequestObject> >("api/GetAdressFromZipCode", request).Result; BaseResponse <ZipCodeResponseObject> result = response.Content.ReadAsAsync <BaseResponse <ZipCodeResponseObject> >().Result; ZipCodeResponseObject returnModel = new ZipCodeResponseObject { Adress = result.ResponseObject.Adress.ToString() }; return(View("Index", returnModel)); }