MVC 4路由

编程入门 行业动态 更新时间:2024-10-17 17:21:29
本文介绍了MVC 4路由-忽略图像请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试阻止用户在我的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路由

本文发布于:2023-10-31 13:23:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546209.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!