使用 mvc/路由模块时,每个请求都会调用 Http 模块

发布于 2024-08-06 17:56:27 字数 1226 浏览 4 评论 0原文

我正在开发一个 http 模块,该模块通过 Authenticate 事件挂钩到 FormsAuthentication 模块。

在调试时,我注意到每次客户端请求资源时(以及当它请求图像、样式表、javascript 文件(等)时),该模块(以及注册的所有其他模块)都会被命中。 当在集成管道模式下在 IIS 7 服务器上运行时,以及通过 webdev 服务器(在非集成管道模式下)进行调试时,都会发生这种情况,

因为我正在开发一个包含大量图像的网站,这些图像通常不会被客户端浏览器缓存,所以会发生这种情况。多次不必要地点击模块。

我正在使用 MVC 及其路由机制(System.Web.Routing.UrlRoutingModule)。 创建新网站时,IIS 7 (system.webServer) 部分的 runAllManagedModulesForAllRequests 属性在 web.config 中默认设置为 true,顾名思义,它会为每个请求调用所有模块。

如果我将 runAllManagedModulesForAllRequests 属性设置为 false,则不会调用任何模块。

似乎这是因为路由模块或mvc(不知道确切原因),这导致asp.net(aspx)处理程序永远不会被调用,因此事件和模块永远不会被调用(仅一次)就像假设的那样)。

我通过尝试调用“mydomain.com/Default.aspx”而不是“mydomain.com/”来测试这一点,并且正确地它只像预期的那样调用模块一次。

我如何解决这个问题,以便它只在请求页面时调用模块一次,而不是在请求所有其他资源时调用模块?

有什么方法可以注册所有请求都应该触发 asp.net (aspx) 处理程序,除了特定文件类型扩展名的请求之外? 当然,如果我选择使用像 /content/images/myimage123 这样的 URL 来获取图像(不带扩展名),这并不能解决问题。但我想不出任何其他方法来解决它。

有没有更好的方法来解决这个问题?

我尝试设置像这样的ignoreRoute:routes.IgnoreRoute("content/{*pathInfo}");其中内容文件夹包含单独子文件夹中的所有图像、javascript 和样式表,但它似乎没有改变任何内容。

在设置处理程序时,我可以看到有很多不同的可能性,但我似乎无法弄清楚应该如何设置一个处理程序,以便可以使用路由模块并具有像 /blog/post123 这样的 url,而不是调用请求图像、JavaScript 和样式表(等)时的模块。

希望有人可以帮助我吗?

马丁

I am developing a http module that hooks into the FormsAuthentication Module through the Authenticate event.

While debugging i noticed that the module (and all other modules registered) gets hit every single time the client requests a resource (also when it requests images, stylesheets, javascript files (etc.)).
This happens both when running on a IIS 7 server in integrated pipeline mode, and debugging through the webdev server (in non- integrated pipeline mode)

As i am developing a website with a lot images which usually wont be cached by the client browser it will hit the modules a lot of unnessecary times.

I am using MVC and its routing mechanishm (System.Web.Routing.UrlRoutingModule).
When creating a new website the runAllManagedModulesForAllRequests attribute for the IIS 7 (system.webServer) section is per default set to true in the web.config, which as the name indicates make it call all modules for every single request.

If i set the runAllManagedModulesForAllRequests attribute to false, no modules will get called.

It seems that the reason for this is because of the routing module or mvc (dont know excactly why), which causes that the asp.net (aspx) handler never gets called and therefore the events and the modules never gets called (one time only like supposed).

I tested this by trying to call "mydomain.com/Default.aspx" instead of just "mydomain.com/" and correctly it calls the modules only once like it is supposed.

How do i fix this so it only calls the modules once when the page is requested and not also when all other resources are requested ?

Is there some way i can register that all requests should fire the asp.net (aspx) handler, except requests for specific filetype extensions ?
Of course that wont fix the problem if i choose to go with urls like /content/images/myimage123 for the images (without the extension). But i cant think of any other way to fix it.

Is there a better way to solve this problem ?

I have tried to set up an ignoreRoute like this routes.IgnoreRoute("content/{*pathInfo}"); where the content folder contains all the images, javascripts and stylesheets in seperat subfolders, but it doesnt seem to change anything.

I can see there a many different possibilites when setting up a handler but I cant seem to figure out how it should be possible to setup one that will make it possible to use the routing module and have urls like /blog/post123 and not call the modules when requesting images, javascripts and stylesheets (etc.).

Hope anyone out there can help me ?

Martin

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

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

发布评论

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

评论(2

鱼忆七猫命九 2024-08-13 17:56:27

问题似乎出在路由模块上。

解决方案是将图像、CSS、JS 移动到子域,或者您可以注册路由模块应忽略的文件类型/扩展名。

The problem seems to be the routing module.

The solution is to move images, css, js to a subdomain, or you can probably register which filetypes/extensions the routing module should ignore.

樱桃奶球 2024-08-13 17:56:27

以下代码是我在每个 MVC 应用程序中使用的代码,以避免路由系统在提供静态文件、javascript、css 等方面造成的开销:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.RouteExistingFiles = false;
    routes.LowercaseUrls = true;
    routes.AppendTrailingSlash = true;
    routes.IgnoreRoute("Content/{*pathInfo}");
    routes.IgnoreRoute("Scripts/{*pathInfo}");
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

    /* ... */
}

The following code is what I use in every MVC Application in order to avoid the overhead caused by the routing system on serving static files, javascript, css, etc:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.RouteExistingFiles = false;
    routes.LowercaseUrls = true;
    routes.AppendTrailingSlash = true;
    routes.IgnoreRoute("Content/{*pathInfo}");
    routes.IgnoreRoute("Scripts/{*pathInfo}");
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

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