/查看/"当URL与&QUOT启动ASP.NET MVC IgnoreRoute方法不能正常工作; [英] ASP.NET MVC IgnoreRoute method doesn't work correctly when URL starts with "/Views/"

查看:109
本文介绍了/查看/"当URL与&QUOT启动ASP.NET MVC IgnoreRoute方法不能正常工作;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果
我使用ASP.NET MVC我的应用程序。结果
用户可以通过包括在网页上注明自己的图像,样式,脚本。结果
但是,当他们指定URL到不存在,那么路由机制,试图找到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.

我添加了一个方法IgnoreRoute并指定有所有扩展我不想通过路由来处理。

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

它正常工作,直到URL不以查看/ ...开头。结果
在这种情况下URL传递到应用程序并执行应用程序内部错误404。结果
但我想处理此错误与IIS。

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.

这可以用空项目进行测试。
你可以简单地使用code代表的Global.asax.cs文件:

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)
        {

        }
    }
}

现在,我们需要在主办在IIS这个应用程序,例如的http://本地主机/ testmvc /

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

您可以放置​​一个破发点的Application_Error方法里面看到错误时,应用程序内部执行

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

所以,现在开放测试网址:
HTTP://localhost/testmvc/test.css 结果
我们可以看到,IIS处理这个错误:

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

现在我们在路径打开另一个URL测试/查看/ ...:
HTTP://localhost/testmvc/Views/test.css 结果
而且我们看到,错误是由ASP.NET处理:

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:

所以,问题是:也许存在一些设置,告诉MVC不与路径中的视图

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

推荐答案

MVC默认情况下不会让你直接访问,因为所有文件类型为System.Web.HttpNotFoundHandler的映射/浏览文件夹下的项目。

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.

要解决这个改变你/Views/web.config你的定义,告诉它基本上忽略该位置一切

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"/>

我写了基于此博客条目​​,因为如果你要包括多个文件类型IIS 6比7的不同。看到:
http://completedevelopment.blogspot.com/2011/06/using-views-outside-of-views-or-other.html

这篇关于/查看/&QUOT;当URL与&QUOT启动ASP.NET MVC IgnoreRoute方法不能正常工作;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆