问题
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:
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:
So the question is: maybe there exists some setting to tell MVC to not handle URL with "Views" in the path?
回答1:
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
回答2:
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!
来源:https://stackoverflow.com/questions/6186008/asp-net-mvc-ignoreroute-method-doesnt-work-correctly-when-url-starts-with-vie