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) ?

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
Yes It requires a key.
If you 'll see in the above url then you 'll get ?key=mykey&
What is the error?
Most likely there is a bug in your upgraded code. Have you tried stepping through the code with the debugger? Can you explain the detailed troubleshooting steps you're performed up to this point?
Yes, I tried to debug , please see above for detail.
Come on man! The error is very clear.
Your sample code does not show where postcode is set! Again, run your code through the debugger. What is the postcode value??? Run the .NET 5 app trough the debugger and also find the postcode value. Are the values the same?
You have to understand the community cannot run your code. You'll need to make an effort to troubleshoot your code. The best thing you can do at this point is assume there is a bug in your code rather than blaming a .NET 6 upgrade.
I think there is a big confusion at your side AgaveJoe.
If you 'll see in the API Url. If I added the authorized key then it is 100% working in my Asp.net mvc 5 code. Yes same postcode is returning many addresses even If I paste the url in the browser then it gets the data/Addresses.
Same postcode is not working in asp.net mvc core 6.
Error description seems to be like there is no record against this post code. wait let me send the addresses here.
I don't think so... Think about this logically. Web browser and .NET 5 application make an HTTP GET request and the server returns data. The .NET 6 application makes a request but the same server returns an error. That means there is a problem with the URL.
Compare the .NET 5 URL with the.NET 6 URL. Please debug your code. If you want the community to debug your code than you'll need to provide a copy of the code the community can execute.
The error doesn't indicate that the Url is wrong. Error is saying post code doesn't have any address.
Why do i want the community to debug my code?
I am already debugging my code. see in the attached snapshots.
.Net 4 and .Net 4.5 allow the user to make this service/api request with same Url then how would I know it 'll stop working in .Net 6 ?
Sign in to comment
The error means the url is not correctly formed.
Do a print from each app and compare.
Hi Bruce, No it is not the case.
If you 'll see in the API Url. If I added the authorized key then it is 100% working in my Asp.net mvc 5 code. Yes same postcode is returning many addresses even If I paste the url in the browser then it gets the data/Addresses.
Same postcode is not working in asp.net mvc core 6.
Error description seems to be like there is no record against this post code. wait let me send the addresses here.
Hi Bruce,
I didn't focused on the Api Url & .
When I remove these from the api url then It is working fine.
But One strange thing is it is working in Asp.Net MVC 5 application.
Bruce, yes Url was not correct.
thank you
Sign in to comment
1 additional answer
Sort by: Most helpful
The error means the url is not correctly formed.
Do a print from each app and compare.
Sign in to comment
Activity