如何检查当前请求的资源是在C#ASP.NET页? [英] How to check current request resource is a page in C# ASP.NET?

查看:188
本文介绍了如何检查当前请求的资源是在C#ASP.NET页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与挂接到 PostAcquireRequestState 一个委托的方法,为每个HTTP请求的 IHttpModule的的实施,我想要知道如何检查当前请求的资源是一个页面(ASPX)识别所有其他资源,如 *。CSS *。ICO *。PNG 等。

I have an IHttpModule implementation with a delegated method hooked to PostAcquireRequestState, for each HTTP Request, I would like to know how to check if the current requested resource is a Page (aspx) discriminating all other resources like *.css, *.ico, *.png and so on.

其实我可以做到以下几点:

Actually I can do the following:

private static void OnPostAcquireRequestState(object sender, EventArgs e)
{
  bool isPage = HttpContext.Current.Request.Path.EndsWith(".aspx");
}

不过,我想知道如果有什么不同的比的.aspx难以检查的事情。

But I would like to know if there is something different to do than hard checking with ".aspx".

推荐答案

有一件事你可以做的是获得注册的 HTTP处理程序并检查它们是否由系统类处理。假设你没有命名空间中的系统命名自己的类* ,这是很简单的:

One thing you could do is to get a list of registered HTTP Handlers and check whether they are handled by a system class. Assuming you don't name your own classes in a namespace System.*, this is quite foolproof:

using System.Configuration;
using System.Web.Configuration;

Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
HttpHandlersSection handlers = (HttpHandlersSection) config
                               .GetSection("system.web/httpHandlers");

List<string> forbiddenList = new List<string>();

// next part untested:
foreach(HttpHandlerAction handler in handlers.Handlers)
{
    if(handler.Type.StartsWith("System."))
    {
        forbiddenList.Add(handler.Path);
    }
}

另外,您也可以恢复查找和列表的所有的现有的处理程序,除了那些在你自己(或电流)域,可能提供了一些例外情况(即,如果要覆盖现有的图像处理程序) 。但无论你选择,这给你完全访问什么的已经注册。

Alternatively, you can revert the lookup and list all existing handlers except those in your own (or current) domain, possibly provided some exceptions (i.e., if you want to override an existing image handler). But whatever you choose, this gives you full access to what's already registered.

请注意:它通常是比较容易做反向。现在,似乎想黑名单一对夫妇的路径,但相反,如果你能做到白名单(即让那些你的扩展名列表做的要处理),你可以自己很多使它更容易。

Note: it's generally easier to do the reverse. You now seem to want to blacklist a couple of paths, but instead, if you can do whitelisting (i.e., make a list of those extensions that you do want to handle) you can make it yourself a lot easier.

这篇关于如何检查当前请求的资源是在C#ASP.NET页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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