MVC 4 routing - ignore requests for images

ぐ巨炮叔叔 提交于 2019-12-13 02:47:33

问题


I'm trying to prevent users from making requests to access images on their own in my web application, so the user shouldn't be allowed to make this type of request:

sitename/Images/test.jpg

I've tried specifying a route to ignore in RouteConfig.cs but it still serves images on their own, is there a way to prevent this?

This is what I've tried:

routes.IgnoreRoute("{file}.jpg");
routes.IgnoreRoute("Images/{file}.jpg");
routes.IgnoreRoute("{resource}.jpg");
routes.IgnoreRoute("Images/{resource}.jpg");

回答1:


If you want to ignore folder you can write like this:

routes.IgnoreRoute("Images/{*pathInfo}");

if you want to ignore only certain files in folder you can use this overload of IgnoreRoute() like this:

routes.IgnoreRoute("{Images}", new { assets = @".*\.(jpeg|gif|jpg)(/.)?" });

As you can see constraints parameter can be implemented like RegExp.



来源:https://stackoverflow.com/questions/32242735/mvc-4-routing-ignore-requests-for-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!