识别客户端是否正在请求页面或 ASP.NET MVC 自定义 HttpModule 中的其他内容?

发布于 2024-10-25 12:47:58 字数 532 浏览 2 评论 0原文

我编写了自定义 HttpModule,每次触发 RequestEnd 事件时都会执行某些操作。

public void Init(HttpApplication context) {
    context.EndRequest += EndEventHandler;
}

但我希望它仅在针对 html 页面的请求触发 EndRequest 事件时执行某些操作。当请求 CSS 文件、图片或其他内容时则不然。我如何识别该特定请求中请求的内容类型,以便我决定要做什么?


注意:我想以前已经有人问过类似的问题,但我找不到它,如果可以的话请添加到可能的重复项。


编辑:更准确地说,我想采取一些如果该请求是由控制器操作处理的,那么请求结束时的步骤(嗯,当我现在考虑时,可能为所有操作调用的操作过滤器会比模块更好 - 是否有某种类型的过滤器在重定向操作时不会被调用被退回?)。

I have written custom HttpModule that does something every time RequestEnd event gets fired.

public void Init(HttpApplication context) {
    context.EndRequest += EndEventHandler;
}

But I want it to do something only when EndRequest event is fired for the request of a html page. Not when it is a request for CSS file, picture or something else. How can I recognize what kind of content is being requested in that particular request so that I can decide what to do ?


Note: I guess similar question has been asked before but I cannot find it please add to possible duplicates if you can.


EDIT: To be more exact, I want to take some steps during the end of request if that request was processed by controller action (hmm when I think about it now maybe action filter that gets called for all actions would be better then module - is there some kind of filter that is not called when redirect action is returned?).

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

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

发布评论

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

评论(1

彼岸花ソ最美的依靠 2024-11-01 12:47:58

您可以查看内容类型:

if (HttpContext.Current.Response.ContentType == "text/html")
{
    ... you will be returning an HTML
}

或者如果您只想限制为静态 HTML 页面,您也可以查看请求:

if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith(".html"))
{
    ... it was a static html page that was requested
}

更新:

好吧,我刚刚注意到您的问题已用 asp.net-mvc 标记。起初,当我看到 HttpModule 时,我以为您正在做一个普通的 ASP.NET 应用程序(我什至无法想象 MVC 应用程序中的 HttpModule)。

现在这已经很清楚了,您显然可以使用 全局操作过滤器,您可以在其中重写 OnActionExecutingOnActionExecuted 方法,这两个方法将分别在执行控制器操作之前和之后调用。

至于您对 OnActionExecuted 方法中的重定向操作的问题,您可以查看 filterContext.Result 并查看它是否是 RedirectToRouteResult 类型:

public class MyActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);
        if (!(filterContext.Result is RedirectToRouteResult))
        {
            ...
        }
    }
}

You can look at the content type:

if (HttpContext.Current.Response.ContentType == "text/html")
{
    ... you will be returning an HTML
}

or if you want to restrict only to static HTML pages you could also look at the request:

if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith(".html"))
{
    ... it was a static html page that was requested
}

UPDATE:

Alright, I've just noticed that your question was tagged with asp.net-mvc. At first when I saw an HttpModule I thought you were doing a normal ASP.NET application (I couldn't even imagine an HttpModule in an MVC application).

Now that this has been clear you obviously could use a global action filter in which you can override the OnActionExecuting and OnActionExecuted methods which will be invoked respectively before and after a controller action is executed.

As far as your question about the redirect action in the OnActionExecuted method you could look at the filterContext.Result and see if it is a RedirectToRouteResult type:

public class MyActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);
        if (!(filterContext.Result is RedirectToRouteResult))
        {
            ...
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文