I have a page in my MVC site that is used to browse through a folder structure on the server, but the wild card mapping is not working for documents.
The mapping works for folder names such as
http://localhost:4321/Document/Folder/General_HR
Which maps to a shared drive folder like T:\root\General_HR
in the controller.
But I get a 404 and the controller method isn't hit when trying to access a file such as
http://localhost:4321/Document/Folder/General_HR/Fire_Drill_Procedures.doc
Here is the route mapping
routes.MapRoute("Document Folder",
"Document/Folder/{*folderPath}",
new { controller = "Document", action = "Folder" });
I also have the standard MVC route that is applied after the one above:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I tried commenting out all other routes, but it still doesn't work.
I had had it working at one point, but I don't know what has changed since then. It seems that it's looking for a resource located at C:\MyProject\General_HR
instead of being routed to the controller. How can I fix my mapping to account for this? According to this answer it should be working.
My controller method works like this: if the URL parameters contains an extension, then I return File(filePath)
, otherwise I create a view model and return View(viewModel)
. So when I click on something with an extension, it should still point to this same controller method and return the file.
public virtual ActionResult Folder(string folderPath)
{
var actualFolderPath = folderPath;
if (string.IsNullOrEmpty(actualFolderPath))
{
actualFolderPath = DocumentPathHelper.RootFolder;
}
else
{
actualFolderPath = DocumentPathHelper.GetActualFileLocation(actualFolderPath);
}
if (System.IO.File.Exists(actualFolderPath))
{
return File(actualFolderPath, MimeMapping.GetMimeMapping(actualFolderPath));
}
var vm = new FolderViewModel();
//build vm
return View(vm);
}