13

我刚刚学习在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 之类的链接,它不应该允许我根据我目前所了解的内容显示该文件,但它确实会显示它。

我究竟做错了什么?

4

2 回答 2

17

忽略 MVC 中的路由将告诉 MVC 框架不要选择这些 URL。

这意味着它将让底层的 ASP.NET 处理请求,这将愉快地向您显示一个静态文件。

于 2013-03-13T09:20:06.197 回答
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>
于 2013-03-13T09:22:23.770 回答