本文介绍了忽略ASP.NET MVC路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚学与路由的工作,我试图了解IgnoreRoute方法。

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

我试图访​​问到prevent用户内容/ {文件名}。html的。我把这个作为我的RegisterRoutes方法中的第一个电话。这里是我的code:

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:

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" }
   );
}

如果我尝试访问本地主机像一个链接:53907 /内容/ Static.html,它不应该让我来显示到目前为止,我了解了文件,但它确实显示它

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.

我在做什么错了?

推荐答案

在MVC忽略路线会告诉MVC框架不拿起这些网址。

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

这意味着它将让底层ASP.NET处理请求,这会很乐意告诉你一个静态文件。

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

这篇关于忽略ASP.NET MVC路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 20:09