I have written custom HttpModule
that does something every time RequestEnd
event gets fired.
public void Init(HttpApplication context) {
context.End开发者_Python百科Request += 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?).
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))
{
...
}
}
}
精彩评论