当 URL 以“/Views/”开头时,ASP.NET MVC IgnoreRoute 方法无法正常工作
我在我的应用程序中使用 ASP.NET MVC。
用户可以通过将其包含在页面上来指定自己的图像、样式、脚本。
但是,当他们指定不存在的文件的 URL 时,路由机制会尝试通过图像或样式等的 URL 查找控制器和操作。
我添加了一个方法 IgnoreRoute 并指定了我不想通过路由处理的所有扩展。
它可以正常工作,直到 URL 不以“Views/...”开头。
在这种情况下,URL 传递到应用程序并在应用程序内部执行错误 404。
但我想用 IIS 来处理这个错误。
这可以用空项目进行测试。 您可以简单地将此代码用于 Global.asax.cs 文件:
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)
{
}
}
}
现在我们需要在 IIS 上托管此应用程序,例如 http: //localhost/testmvc/
您可以在 Application_Error 方法内部放置一个断点,以查看错误何时在应用程序内部执行
所以现在打开测试 URL: http://localhost/testmvc/test.css
我们可以看到 IIS 处理了该错误:
现在我们打开另一个测试 URL,路径中包含“/Views/...”: http://localhost/testmvc/Views/test.css
我们看到错误是由 ASP.NET 处理的:
所以问题是:也许存在一些设置告诉 MVC 不要处理带有“Views”的 URL路径?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,MVC 不允许您直接寻址 /Views 文件夹下的项目,因为所有文件类型都映射到 System.Web.HttpNotFoundHandler。
为了解决这个问题,请更改 /Views/web.config 中的定义,告诉它忽略该位置中的其他所有内容,
我基于此编写了一篇博客文章,因为如果您想包含多种文件类型,IIS 6 与 7 不同。看:
http://completedevelopment.blogspot.com /2011/06/using-views-outside-of-views-or-other.html
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
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
这是我的方法:
1-在 Views 文件夹中创建一个新文件夹,例如。 myFolder
2- 将静态页面添加到这个新文件夹中,例如。 filename.cshtml
3-从“Views”文件夹中复制 web.config 文件并将其粘贴到您刚刚创建的新文件夹中
创建(myFolder)
4- 在新的 web.config 中替换
这个:
用这个:
5-如果找到这些行,请删除它:
结果:
现在,该文件夹中的任何文件都无需路由即可工作!
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 :
with this :
5- delete these line if you found it :
Resault :
Now any file in this folder will work without routing!