Map route with File Extention to Action in controller?

I have built out a function so I can dynamically resize images for display and while building out the Route I would use to call the images it seems that if there is a file extension at the end, say /Folder/250/250/image.jpg MVC tries to match it to a file
in a real path and throws a 404 when it does not find it. Is there a way to shut off this behavior so that if the path ends with a file extension it will always map to my function?

see

routes.MapExistingFiles=false

ignatandrei

see

routes.MapExistingFiles=false

Completely missed that. Thanks!

ignatandrei

see

routes.MapExistingFiles=false

I spoke too soon, does not seem to be working

Eagle_f90

ignatandrei

see

routes.MapExistingFiles=false

I spoke too soon, does not seem to be working

Yes, it works. Please read

http://weblogs.asp.net/jongalloway/asp-net-mvc-routing-intercepting-file-requests-like-index-html-and-what-it-teaches-about-how-routing-works

Have you tested this your self and gotten it working? It is not working for me. I have tried both false and true and I get a 404 file not found when the path has an extension. I know the app is working and the URL is mapped correctly since if I remove the
extension the app will fail to find the specified image on the local system. I can also only change the route so that the file name does not come last and everything works 100%

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.RouteExistingFiles = false;

By adding

<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

to the web.config I have gotten this to work in debug mode using IIS Express, but when I publish to IIS 8.5 it still fails.

After testing a bit more it looks like ASP.Net is completely ignoring the routes.RouteExistingFiles attribute. If I comment it out but leave the my update in the web.config the files still get serviced.

Hi Eagle,

Base on my test, I found that the RouteExistingFiles not work in the MVC 5 project (Maybe the behavior has changed or something issue in my project).

For your requirement, please add a custom route (Befault the default route), such as:

routes.MapRoute(
      name: "tt",
      url: "{*src}",
        defaults: new { controller = "Home", action = "Contact", id = UrlParameter.Optional },
       constraints: new { src = @"(.*?).(html|htm|aspx|asp|jpg)" },
       namespaces: new string[] { "XXX" }
         );

If it still have the issue, please share the project on the OneDrive. (You may reproduce that scene in a new project and share it)

Best Regards

Starain

Starain chen – MSFT

Hi Eagle,

Base on my test, I found that the RouteExistingFiles not work in the MVC 5 project (Maybe the behavior has changed or something issue in my project).

For your requirement, please add a custom route (Befault the default route), such as:

routes.MapRoute(
      name: "tt",
      url: "{*src}",
        defaults: new { controller = "Home", action = "Contact", id = UrlParameter.Optional },
       constraints: new { src = @"(.*?).(html|htm|aspx|asp|jpg)" },
       namespaces: new string[] { "XXX" }
         );

If it still have the issue, please share the project on the OneDrive. (You may reproduce that scene in a new project and share it)

Best Regards

Starain

I don’t think it is an issue with your project as my project is an MVC 5 as well. I will give this a shot when I return home tonight but just for clarification when you have

url: "{*src}",

I would just leave my full route in tact and update the constraints to

constraints: new { FileName = @"(.*?).(html|htm|aspx|asp|jpg)" },

Hi Eagle,

Eagle_f90

constraints: new { FileName = @"(.*?).(html|htm|aspx|asp|jpg)" },

For this constraints, please modify the URL value: {“*FileName”}

If you have that issue in your MVC 5 project, I think that route won’t work. It will access the file directly.

I suggest that you could submit this feedback to Microsoft Connect feedback portal:
http://connect.microsoft.com, Microsoft engineers will evaluate them seriously. Thanks for your understanding. After you submit the feedback, you can post the link here which will be beneficial for other members with
the similar issue.  

Best Regards

Starain

Starain chen – MSFT

Hi Eagle,

Eagle_f90

constraints: new { FileName = @"(.*?).(html|htm|aspx|asp|jpg)" },

For this constraints, please modify the URL value: {“*FileName”}

If you have that issue in your MVC 5 project, I think that route won’t work. It will access the file directly.

I suggest that you could submit this feedback to Microsoft Connect feedback portal:
http://connect.microsoft.com, Microsoft engineers will evaluate them seriously. Thanks for your understanding. After you submit the feedback, you can post the link here which will be beneficial for other members with
the similar issue.  

Best Regards

Starain

I had the same problem of it trying to serve up a file via the IIS Static file handler. I will leave feed back as suggested, thanks.

Hi Eagle,

After you submit the feedback, please post the link here.

Best Regards

Starain

Leave a Reply