0

I'm trying to set one controller so it can handle all of these urls:

/file/filename.jpg
/file/path1/filename.jpg
/file/path1/path2/filename.jpg

etc.

The individual values for path1, path2, etc, will be different, and the filename.jpg will be different as well.

I found a couple of other questions that are sort of similar but don't quite work for me.

Multiple levels in MVC custom routing

My current routing for this is:

routes.MapRoute(
    name: "BlobStorage",
    url: "File/{*filepath}",
    defaults: new { controller = "File", action = "Index", filepath = UrlParameter.Optional },        
    namespaces: new string[] { "WebApp.Controllers" }
);

This sort of works, in that if I use the url /file/path1/blarg, the controller will hit. But if I put an extension on the blarg, it won't. /file/path1/blarg.jpg just gives me a 404.

I really hate to set up a bunch of index methods in the controller just to handle multiple levels of paths.

Any ideas?


Ok so it looks like the routing is correct but it's the file extension that is the problem.

I tried setting it up with a number of methods to test for /path1/filename, /path1/path2/filename, /path1/path2/path3/filename, and I'm getting the same issue as with the routing code. The problem seems to be that if I include an extension, the routing won't work. But it works fine if I don't include an extension.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • If these are arbitrary paths then could you not use the route tag: [Route("file/path1/path2/filename.jpg")] either in your controller or on specific action? – Glynn Hurrell May 04 '20 at 19:46
  • I would need to add file/path1/filename.jpg and file/path1/path2/filename.jpg, etc, for each level. I'm tryign to avoid that. –  May 04 '20 at 19:54

0 Answers0