One of my API is working in Asp.Net MVC 5 But Now Api is not working When I am using it in Asp.Net MVC Core (6) ?

mehmood tekfirst 771 Reputation points
2022-08-26T06:35:34.917+00:00

Hi,

One of my code is perfectly working in Asp.Net MVC 5 But when I am using some logic to call the api in Asp.Net MVC Core (6), Api is always returning an error.

Same parameters and same address is being used in both technologies but old approach is working.

Please see my Old and New Logic to call this api. and guide me to fix it.
The Api Url is :

https://pcls1.craftyclicks.co.uk/json/rapidaddress?key=mykey&include_geocode=true&&postcode=DD68AB&response=data_formatted  

Asp.Net MVC 5

using (var client = new HttpClient())  
                {  
                    string addressUri = WebConfigurationManager.AppSettings["addressUri"];  
                    var uri = addressUri + postcode.Replace(" ","") + "&response=data_formatted";  
                    var response = await client.GetAsync(uri);  
                    string textResult = await response.Content.ReadAsStringAsync();  
                    lObjApiRes = JsonConvert.DeserializeObject<AddressLookupViewModel>(textResult);  
                }  

AspNet MVC Core (6)

                AddressLookupViewModel? nullableApiResObj = null;  
                Task<Stream> streamTask = client.GetStreamAsync(uri);  
                nullableApiResObj = await JsonSerializer.DeserializeAsync<AddressLookupViewModel?>(await streamTask);  

This approach return everything null and doesn't throw any error

I tried another solution, please see below

using (HttpClient client = new HttpClient())  
            {  
                string addressUri = Configuration["CustomSettings:AddressUri"];  
                string uri = addressUri + postcode.Replace(" ", "") + "&response=data_formatted";  
                string textResult = "";  
  
                HttpRequestMessage request = new HttpRequestMessage()  
                {  
                    RequestUri = new Uri(uri),  
                    Method = HttpMethod.Get,  
                };  
                HttpResponseMessage response = await client.SendAsync(request);  
                if (response.IsSuccessStatusCode)  
                {  
                    textResult = await response.Content.ReadAsStringAsync();                        
                     
                }  
        }  

The steps are simple to reproduce the case. Just create an Asp.Net MVC Core (.Net 6) project and try my code.

Please guide me to resolve the issue in a better way.

Why I am always getting this error:

textResult always return {"error_code":"0001","error_msg":"No data was found for the requested postcode."}

Thank you

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,211 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 57,166 Reputation points
    2022-08-26T14:37:04.863+00:00

    The error means the url is not correctly formed.

    Do a print from each app and compare.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 57,166 Reputation points
    2022-08-26T14:37:34.637+00:00

    The error means the url is not correctly formed.

    Do a print from each app and compare.

    1 person found this answer helpful.
    0 comments No comments