1

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Anton Palyok
  • 1,249
  • 1
  • 16
  • 27

2 Answers2

3

Here are my way :

1- Make a new folder in Views folder, eg. myFolder

2- Add your static page into this new folder, eg. filename.cshtml

3- Copy the web.config file from "Views" folder and paste it to the new folder you just created (myFolder)

4- In the new web.config Replace this :

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

with this :

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

5- delete these line if you found it :

   <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode"            type="System.Web.HttpNotFoundHandler" />

Resault : Now any file in this folder will work without routing!

3

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

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • I also figured out this. But MVC provides me with HttpException and httpcode=404 for such URLs. So why it can't pass this url to IIS? Maybe you know how to allow accessing only for static files under the /Views folder? Or it is a restriction of MVC and I should handle this situation in Application_Error method? – Anton Palyok May 31 '11 at 18:57
  • Because the response code of 404 is set in .net and the exception is thrown. IIS will not then handle it since the source of this error is inside .net code. In the other case, .Net completely ignores any processing, hence IIS handles it. – Adam Tuliper May 31 '11 at 19:32
  • @Adam Tuliper and facts like those is what makes dealing with the ASP.NET and IIS custom errors handling total voodoo and you can spend days trying to get it to all work perfectly. – Chris Marisic Jun 03 '11 at 14:41
  • ha.. ya. to be fair though.. most would keep other content out of /views - you have a special case here - but I know - its a learning curve when dealing with the ins and outs : ) – Adam Tuliper Jun 03 '11 at 15:07
  • @Adam Tuliper I've also researched this issue and found exactly the same solution as you described in edition of your comment. The culprit turned out to be "BlockViewHandler" in "handler" section of IIS configuration in web.config inside folder "Views" So problem is solved now. Thanks for cooperation. – Anton Palyok Jun 04 '11 at 20:07