13

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

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

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.

What am I doing wrong?

Jhonny D. Cano -Leftware-
  • 17,663
  • 14
  • 81
  • 103
aleczandru
  • 5,319
  • 15
  • 62
  • 112
  • and what is the problem ? – Ravi Gadag Mar 13 '13 at 09:11
  • have you tried? routes.IgnoreRoute("Content/{*pathInfo}.html"); – Jon Mar 13 '13 at 09:12
  • if I try to acces a link like this : http://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 – aleczandru Mar 13 '13 at 09:14
  • after trying : routes.IgnoreRoute("Content/{*pathInfo}.html"); I got this error : A path segment that contains more than one section, such as a literal section or a parameter, cannot contain a catch-all parameter. – aleczandru Mar 13 '13 at 09:16

2 Answers2

17

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

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

René Wolferink
  • 3,558
  • 2
  • 29
  • 43
  • so in other words this will only work if a user clicks on a link that directs him to Content/{filename}.html but if he types the link himself he will have access.Have I udnerstood this corectly? – aleczandru Mar 13 '13 at 09:25
  • @aleczandru No, what you're saying is if someone hits the url Content/{filename}.html with any file, then ignore the route, but by ignoring it, you're passing it to ASP.NET to handle and that's going to route you to the URL regardless. – Mathew Thompson Mar 13 '13 at 09:28
  • what if i write routes.IgnoreRoute("Resources/Desert.jpg"); and make a hyperlink in the view of index method of this image, then i click on hyperlink it will redirect me to the image or it will ignore this ? – Mogli Apr 26 '14 at 11:45
1

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

Place a web.config in that folder.

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>
Yahya
  • 3,386
  • 3
  • 22
  • 40
  • 2
    it's not about blocking access to the folder but about understanding how routing works I know that I can block the access threw web.config , but thanks for the answer anyway – aleczandru Mar 13 '13 at 09:26