我正在学习使用ASP.NET MVC中的路由,并尝试了解IgnoreRoute方法。
我试图阻止用户访问"Content/{filename}.html"
。我把它作为RegisterRoutes方法中的第一个调用。这是我的代码:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("Content/{filename}.html");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^.*", action = "^Index$|^About$" },
new[] { "UrlsAndRoutes.AditionalControllers" });
routes.MapRoute("MyRoute2", "{controller}/{action}/{id}/{*catchall}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^.*", action = "^Index$|^About$" },
new[] { "UrlsAndRoutes.Controllers" });
routes.MapRoute("ShopSchema2", "Shop/OldAction", new { controller = "Home", action = "Index" });
routes.MapRoute("ShopSchema", "Shop/{action}", new { controller = "Home" });
routes.MapRoute("", "X{controller}/{action}");
routes.MapRoute(
name: "",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
}
如果我尝试访问localhost:53907 / Content / Static.html这样的链接,它不应该允许我显示目前为止我所理解的文件,但它会显示它。
我做错了什么?
答案 0 :(得分:17)
忽略MVC中的路由将告诉MVC框架不要接收这些URL。
这意味着它将让底层ASP.NET处理请求,这将很乐意向您显示静态文件。
答案 1 :(得分:1)
如果你真的想阻止对该文件夹的访问,为什么不在web.config中定义它?
将web.config放在该文件夹中。
内容应为:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authorization>
<!-- <allow roles="admin" /> --> //In case you want to give access to admin only.
<deny users ="*" />
</authorization>
</system.web>
</configuration>