Issue
I moved my ASP.NET Core 6 MVC Web application from IIS to Ubuntu 22.04 using Apache2 as the webserver. The only issue I have is the select dropdown is not showing the pdf name files in order after moving the web app to Ubuntu. When using localhost in visual studio files show in order and IIS in Windows on a live server.
I'm not sure why this is happening. Is it an Ubuntu issue. Below is the method to add the files to the select dropdown.
Any help would be much appreciated.
public static UserFile GetPdfFiles(UserFile userFile, string webRootPath, string fileDirectoryPath)
{
string fullPath = $"{webRootPath}{fileDirectoryPath}";
int nId = 1;
userFile.Files = new List<SelectListItem>();
foreach (string pdfPath in Directory.EnumerateFiles(fullPath, "*.pdf"))
{
userFile.Files.Add(new SelectListItem
{
Text = Path.GetFileName(pdfPath),
Value = nId.ToString(),
});
nId++;
}
int listCount = userFile.Files.Count - 1;
userFile.Name = userFile.Files[listCount].Text;
userFile.Path = fileDirectoryPath.Replace('\\', '/');
return userFile;
}
Ubuntu
Windows/locahost
Solution
The Directory.EnumerateFiles()
method returns items on the Windows and on the Ubuntu in a different order. In the Windows files already sorted by ascendance, but seems like on the Ubuntu files returned in order they was created (not sorted).
Therefore if want to show files in specific order it is necessary to sort them. For example, to sort by ascendance:
foreach (string pdfPath in Directory.EnumerateFiles(fullPath, "*.pdf").Select(f => Path.GetFileName(f)).OrderBy(o => o))
{
userFile.Files.Add(new SelectListItem
{
Text = pdfPath,
Value = nId.ToString(),
});
nId++;
}
Answered By - Victor Answer Checked By - Robin (WPSolving Admin)