0

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);
    }
Community
  • 1
  • 1
DLeh
  • 23,806
  • 16
  • 84
  • 128
  • What are your other routes that are defined, and in what order? – danludwig Nov 26 '14 at 14:55
  • I've added the other route that I'm using that could conflict – DLeh Nov 26 '14 at 14:57
  • Is this the only other route? Other routes that could possibly interfere would come before this one. – danludwig Nov 26 '14 at 14:59
  • I commented out all other routes, the folder browsing still works but the document browsing still isn't working, so I don't think that it's other routes that are causing the problem – DLeh Nov 26 '14 at 15:01
  • URLs with extensions, i.e. `*.doc` are not sent to the MVC routing framework, but rather are attempted to be served directly by IIS. See this article for a workaround: http://weblogs.asp.net/jongalloway/asp-net-mvc-routing-intercepting-file-requests-like-index-html-and-what-it-teaches-about-how-routing-works. It's geared to serve *.html through the MVC routing framework, but you can follow the same procedure for any file extension. – Chris Pratt Nov 26 '14 at 15:35
  • is there a way around that? or should I try to replace the `.` with a `_` or something? – DLeh Nov 26 '14 at 15:35

1 Answers1

0

I "solved" this by altering my urls to the documents by replacing the . with a placeholder, then replacing it back in the controller.

for each item:

folderPath = folderPath.Replace(".", "___");

Then in the controller:

    public virtual ActionResult Folder(string folderPath)
    {
        var actualFolderPath = folderPath.Replace("___", ".");
DLeh
  • 23,806
  • 16
  • 84
  • 128