I used to have this AngularJS project where I used to call my API at this way:
$http.get(`${uriApi}api/PDS/CheckDirectoryExistance?path=` + encodeURIComponent(dirPath))
.then(response => {
if (response.data.ok) {
docFound = true;
LoadRootLevel(dirPath);
Promise.resolve()
}
/*My Code Continues*/
})
.catch(error => {
if (!alertShown) {
docFolderFound = false;
console.error("Error checking directory existence:", error);
alert("There's no process documentation created for '" + $scope.partNo + "'\nPlease verify that this is correct or ask your supervisor to provide the documentation." +
"Evaluating folders: \n" + dirs2 + "\n\nNo existe documentacion creada para el numero de parte '" + $scope.partNo + "'\nPor favor verifique que esto sea correcto o solucite a su supervisor la creacion de la documentacion"
);
alertShown = true;
}
});
But now I'm migrating my app to use Angular17 and ASP.NET Core. The thing is that, my API didn't change basically. As well here's my API Code:
[HttpGet("CheckDirectoryExistance/{path}")]
public ActionResult CheckDirectoryExistance(string path)
{
try
{
bool directoryExists = Directory.Exists(path);
return Ok(new { ok = directoryExists });
} catch(Exception ex)
{
return NotFound(ex);
}
}
And here is my new TS code calling that API:
let dirs = [];
let dirPath = this.path + this.workSpaceInfo.userCell + "\\" + this.itemsInfo.partNo;
let alertShown = false;
console.log("DirPath? ", dirPath);
try {
let response = await this.http.get(`api/PDSWorker/CheckDirectoryExistance/` + encodeURIComponent(dirPath)).toPromise();
if (response == true) {
docFound = true;
this.LoadRootLevel(dirPath);
console.log("directory found from response");
}
/* My code Continues*/
} catch (error) {
console.error("Error checking directory existence general catch:", error);
}
The problem is:
I'm getting this error and I cannot find where's my error:
GEThttps://localhost:44359/api/PDSWorker/CheckDirectoryExistance/%5C%5CSharedFile01%5CPDS%5CX405%5CSt6%5C284A1733-170 404 (Not Found)
Error checking directory existence general catch:
message : "Http failure response for https://localhost:44359/api/PDSWorker/CheckDirectoryExistance/%5C%5CSharedFile01%5CPDS%5CX405%5CSt6%5C284A1733-170: 404 OK"
name: "HttpErrorResponse"
ok: false
status: 404
statusText: "OK"
url:"https://localhost:44359/api/PDSWorker/CheckDirectoryExistance/%5C%5CSharedFile01%5CPDS%5CX405%5CSt6%5C284A1733-170"
I have already tried next:
After a long research I've noticed what my problem was: My directory path has to be encoded being like this:
let dirPath = this.path + this.workSpaceInfo.userCell + "\\" + this.itemsInfo.partNo;
let encondedURL = encodeURIComponent(dirPath);
And I add that new variable to my API call:
let response = await this.http.get<any>(`api/PDSWorker/CheckDirectoryExistance?path=` + encondedURL).toPromise();
But the change was in my API actually:
[HttpGet("CheckDirectoryExistance")]
public ActionResult CheckDirectoryExistance([FromQuery] string path)
{
try
{
bool directoryExists = Directory.Exists(path);
return Ok(new { ok = directoryExists });
} catch(Exception ex)
{
Console.WriteLine("Error while Verifying the directory existance");
return BadRequest(ex);
}
}
Notice that I removed the "{path}" from the route and I added "[FromQuery]". This because I was passing the string as a query parameter and I should use it as a route parameter.