General handler for ASP.NET MVC queries
I need to create a general handler for all ASP.NET MVC queries. Now I use Application_BeginRequest but there come not only ASP.NET MVC queries.
The one option I see is to create a common base controller and execute my logic in its constructor. But may be th开发者_运维百科ere are other options?
Have you considered action filters?
You have to add ActionFilter to your controller.
In order to do this you have to create an class inherited from ActionFilterAttribute For example:
Public Class CustomFilterAttribute
Inherits ActionFilterAttribute
End Class
Than just apply this attribute to controller:
<CustomFilter()> _
Public Class MyController
There are 4 methods in ActionFilterAttribute which can be overrided:
OnActionExecuted
OnActionExecuting
OnResultExecuted
OnResultExecuting
Override them and this code will be executed on each request to methods of your controller
idsa,
you might be able to rustle something up using this kind of approach in a base controller:
protected override void Initialize(RequestContext requestContext)
{
Lang = requestContext.RouteData.Values["lang"].ToString()
?? System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
ViewData["Lang"] = Lang;
base.Initialize(requestContext);
// your custom logic here...
}
or on:
protected override void Execute(System.Web.Routing.RequestContext requestContext)
{
base.Execute(requestContext);
// intercepting code here...
}
or:
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
// one stage later intercepting code here
}
who knows mind you :)
精彩评论