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?

4
  • and what is the problem ? Commented Mar 13, 2013 at 9:11
  • have you tried? routes.IgnoreRoute("Content/{*pathInfo}.html"); Commented Mar 13, 2013 at 9:12
  • if I try to acces a link like this : 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 Commented Mar 13, 2013 at 9: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. Commented Mar 13, 2013 at 9:16

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.