当 URL 以“/Views/”开头时,ASP.NET MVC IgnoreRoute 方法无法正常工作

发布于 2024-11-10 08:18:19 字数 2043 浏览 6 评论 0原文

我在我的应用程序中使用 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:
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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

压抑⊿情绪 2024-11-17 08:18:19

默认情况下,MVC 不允许您直接寻址 /Views 文件夹下的项目,因为所有文件类型都映射到 System.Web.HttpNotFoundHandler。

为了解决这个问题,请更改 /Views/web.config 中的定义,告诉它忽略该位置中的其他所有内容,

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

我基于此编写了一篇博客文章,因为如果您想包含多种文件类型,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

<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

没企图 2024-11-17 08:18:19

这是我的方法:

1-在 Views 文件夹中创建一个新文件夹,例如。 myFolder

2- 将静态页面添加到这个新文件夹中,例如。 filename.cshtml

3-从“Views”文件夹中复制 web.config 文件并将其粘贴到您刚刚创建的新文件夹中
创建(myFolder)

4- 在新的 web.config 中替换
这个:

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

用这个:

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

5-如果找到这些行,请删除它:

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

结果:
现在,该文件夹中的任何文件都无需路由即可工作!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文