Search code examples
asp.net-mvcasp.net-mvc-routingignoreroute

ASP.NET MVC IgnoreRoute method doesn't work correctly when URL starts with "/Views/"


I use ASP.NET MVC in my application.
Users can specify their own images, styles, scripts by including them on the page.
But when they specify URL to the file which not exists then routing mechanism tries to find controller and action by URL to image or styles etc.

I've added a method IgnoreRoute and specified there all extensions I don't want to handle by routing.

It works correctly until URL doesn't starts with "Views/...".
In this case URL passes into application and executes error 404 inside of application.
But I want to handle this error with IIS.

This can be tested with empty project. You can simply use this code for Global.asax.cs file:


using System;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication1
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.IgnoreRoute(
                "{*staticfile}",
                new { staticfile = @".*\.(jpg|gif|jpeg|png|js|css|htm|html|htc)$" }
            );

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }

        void Application_Error(object sender, EventArgs e)
        {

        }
    }
}

Now we need to host this application at IIS, for example at http://localhost/testmvc/

You can place a break-point inside of Application_Error method to see when error executes inside of application

So now open test URL: http://localhost/testmvc/test.css
We can see that IIS handled that error: enter image description here

Now we open another test URL with "/Views/..." in the path: http://localhost/testmvc/Views/test.css
And we see that error was handled by ASP.NET: enter image description here

So the question is: maybe there exists some setting to tell MVC to not handle URL with "Views" in the path?


Solution

  • MVC by default will not allow you to directly address items under the /Views folder because of the mapping of all file types to the System.Web.HttpNotFoundHandler.

    To get around this change your definition in your /Views/web.config to tell it to ignore basically everything else in that location

    <add path="*.cshtml" verb="*" type="System.Web.HttpNotFoundHandler"/>
    

    I wrote up a blog entry based on this since IIS 6 is different than 7 if you want to include multiple file types. See: http://completedevelopment.blogspot.com/2011/06/using-views-outside-of-views-or-other.html