MVC 4路由-忽略图像请求 [英] MVC 4 routing - ignore requests for images
问题描述
我正在尝试阻止用户在我的Web应用程序中自行请求访问图像,因此不应允许用户提出此类请求:
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
sitename/Images/test.jpg
我尝试在RouteConfig.cs中指定要忽略的路由,但是它仍然自己提供图像,是否在那里一种防止这种情况的方法?
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?
这是我尝试过的方法:
routes.IgnoreRoute("{file}.jpg");
routes.IgnoreRoute("Images/{file}.jpg");
routes.IgnoreRoute("{resource}.jpg");
routes.IgnoreRoute("Images/{resource}.jpg");
推荐答案
如果要忽略文件夹,可以这样写:
If you want to ignore folder you can write like this:
routes.IgnoreRoute("Images/{*pathInfo}");
如果只想忽略文件夹中的某些文件,可以使用此 IgnoreRoute()
像这样:
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)(/.)?" });
如您所见,约束
参数可以是像 RegExp
这样实现。
As you can see constraints
parameter can be implemented like RegExp
.
这篇关于MVC 4路由-忽略图像请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!