ASP.NET MVC 3:RouteExistingFiles =真正似乎没有任何效果 [英] ASP.NET MVC 3: RouteExistingFiles = true seems to have no effect

查看:180
本文介绍了ASP.NET MVC 3:RouteExistingFiles =真正似乎没有任何效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解RouteExistingFiles是如何工作的。
所以,我创建了一个新的MVC 3互联网项目(MVC 4进行同样的操作),并把HTMLPage.html文件到我的项目的内容文件夹。
现在我去Global.asax文件和编辑的RegisterRoutes功能,所以它看起来是这样的:

I'm trying to understand how RouteExistingFiles works. So I've created a new MVC 3 internet project (MVC 4 behaves the same way) and put a HTMLPage.html file to the Content folder of my project. Now I went to the Global.Asax file and edited the RegisterRoutes function so it looks like this:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.RouteExistingFiles = true; //Look for routes before looking if a static file exists

    routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
            );
    }

现在它应该给我一个错误,当我请求本地主机:XXXX /内容/ HTMLPage.html由于没有内容控制器和要求肯定是打的缺省模式。但是相反,我看到我的HTMLPage。
我在做什么错在这里?

Now it should give me an error when I'm requesting a localhost:XXXX/Content/HTMLPage.html since there's no "Content" controller and the request definitely hits the default pattern. But instead I'm seeing my HTMLPage. What am I doing wrong here?

更新:
我想我将不得不放弃。
即使我添加的路由像这样的:

Update: I think I'll have to give up. Even if I'm adding a route like this one:

routes.MapRoute("", "Content/{*anything}", new {controller = "Home", action = "Index"});

它仍然显示我HTMLPage的内容。
当我要求很喜欢〜/内容/ HTMLPage我得到的索引页不如预期,但是当我添加文件extenstion喜欢的.html或.txt显示内容(或者如果文件不存在,一个404错误)的URL 。
如果任何人都可以在VS2012检查此,请让我知道什么导致你得到。
谢谢你。

it still shows me the content of the HTMLPage. When I request a url like ~/Content/HTMLPage I'm getting the Index page as expected, but when I add a file extenstion like .html or .txt the content is shown (or a 404 error if the file does not exist). If anyone can check this in VS2012 please let me know what result you're getting. Thank you.

推荐答案

要启用路由你必须执行以下步骤静态文件。

To enabling routing for static files you must perform following steps.

在RouteConfig.cs启用路由现有文件

In RouteConfig.cs enable routing for existing files

routes.RouteExistingFiles = true;

添加一个路由路径(确保专业道路是广义的路径上)

Add a route for your path ( Make sure specialized path are above generalized paths)

routes.MapRoute(
            name: "staticFileRoute",
            url: "Public/{file}/",
            defaults: new { controller = "Home", action = "SomeAction" }
        );

下一步配置您的应用程序,所以对于静态文件的请求是由TransferRequestHandler。在Webconfig system.webServer下>处理程序添加以下条目handeled。

Next configure your application, so that request for static files are handeled by "TransferRequestHandler".In Webconfig under system.webServer>handlers add following entry.

<add name="MyCustomUrlHandler2" path="Public/*" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

路径的值可以是多个通用或具体取决于你的要求。但我preFER它总是非常具体的根据自己的需要。保持它非常通用将阻止其他网站特定的资源投放,如.js文件或CSS文件。例如,如果上述设置为PATH =*,那么,即使是CSS(内容文件夹中),这是负责为您的网页会怎样看也将在你的控制器的动作结束了要求。什么,你不会喜欢。

The value of 'path' can be more generic or specific depending on your requirement. But i prefer it to be always very specific as per one's need. Keeping it very generic will block serving of other site specific resources such as .js or css files. For example if above is set as path="*", then request for even the css (inside the content folder) which is responsible for how your page would look will also end up in your Controller's action. Something that you will not like.

这篇关于ASP.NET MVC 3:RouteExistingFiles =真正似乎没有任何效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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