C# 忽略 ASP.NET MVC 中的路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15380770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:35:18  来源:igfitidea点击:

Ignoring a route in ASP.NET MVC

c#asp.net-mvcasp.net-mvc-4asp.net-mvc-routing

提问by aleczandru

I am just learning to work with routing in ASP.NET MVCand am trying to understand the IgnoreRoute method.

我刚刚学习在ASP.NET MVC 中使用路由,并试图了解 IgnoreRoute 方法。

I am trying to prevent users from accessing "Content/{filename}.html". I have placed this as the first call in my RegisterRoutes method. Here is my code:

我试图阻止用户访问"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" }
   );
}

If I try to access a link like localhost:53907/Content/Static.html, it should not allow me to display the file from what I understand so far, but it does display it.

如果我尝试访问像 localhost:53907/Content/Static.html 这样的链接,它不应该允许我根据我目前的理解显示文件,但它确实显示了它。

What am I doing wrong?

我究竟做错了什么?

采纳答案by René Wolferink

Ignoring routes in MVC will tell the MVC framework not to pick up those URLs.

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

This means that it will let the underlying ASP.NET handle the request, which will happily show you a static file.

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

回答by Yahya

If you really want to block access to that folder, why not define it in web.config?

如果您真的想阻止对该文件夹的访问,为什么不在 web.config 中定义它?

Place a web.config in that folder.

将 web.config 放在该文件夹中。

The contents should be:

内容应该是:

<?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>