识别客户端是否正在请求页面或 ASP.NET MVC 自定义 HttpModule 中的其他内容?
我编写了自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以查看内容类型:或者如果您只想限制为静态 HTML 页面,您也可以查看请求:
更新:
好吧,我刚刚注意到您的问题已用
asp.net-mvc
标记。起初,当我看到 HttpModule 时,我以为您正在做一个普通的 ASP.NET 应用程序(我什至无法想象 MVC 应用程序中的 HttpModule)。现在这已经很清楚了,您显然可以使用 全局操作过滤器,您可以在其中重写
OnActionExecuting
和OnActionExecuted
方法,这两个方法将分别在执行控制器操作之前和之后调用。至于您对 OnActionExecuted 方法中的重定向操作的问题,您可以查看
filterContext.Result
并查看它是否是RedirectToRouteResult
类型:You can look at the content type:or if you want to restrict only to static HTML pages you could also look at the request:
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
andOnActionExecuted
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 aRedirectToRouteResult
type: