Use of the environment: in the background management or the front desk has a member center in the case of use
Usage: This is a User Center controller I add a feature to the User Center home page
[Accountfilter] This is the name of a filter
Public class Accountcontroller:controller { [accountfilter] public actionresult Index () { return View (); } }
Filter specific code is as follows
namespacefytmsys.helper{/// <summary> ///Foreground User Login filter/// </summary>[AttributeUsage (attributetargets.all, AllowMultiple =true)] Public classAccountfilterattribute:actionfilterattribute { Public stringMessage {Get;Set; } Filtercontextinfo _fcinfo; //called by the ASP. OnActionExecuted after executing the action method. //called by the ASP. OnActionExecuting before executing the action method. //called by the ASP. Onresultexecuted after executing the result of the operation. //Onresultexecuting is called by the ASP. NET before executing the result of the operation. /// <summary> ///called by the ASP framework before the action method is executed. /// </summary> /// <param name= "Filtercontext" ></param> Public Override voidonactionexecuting (ActionExecutingContext filtercontext) {_fcinfo=NewFiltercontextinfo (Filtercontext); if(Utilshelper.getcookie ("Ddhuser") =="") {Filtercontext.result=NewRedirecttorouteresult (NewSystem.Web.Routing.RouteValueDictionary (New{Controller ="Users", action ="Login" })); } } /// <summary> ///called by the ASP framework after the action method is executed. /// </summary> /// <param name= "Filtercontext" ></param> Public Override voidonactionexecuted (ActionExecutedContext filtercontext) {Base. OnActionExecuted (Filtercontext); } /// <summary> ///called by the ASP. Onresultexecuted after executing the result of the operation. /// </summary> /// <param name= "Filtercontext" ></param> Public Override voidonresultexecuted (ResultExecutedContext filtercontext) {Base. Onresultexecuted (Filtercontext); } /// <summary> ///Onresultexecuting is called by the ASP. NET before executing the result of the operation. /// </summary> /// <param name= "Filtercontext" ></param> Public Override voidonresultexecuting (ResultExecutingContext filtercontext) {Base. Onresultexecuting (Filtercontext); } Public classFiltercontextinfo { PublicFiltercontextinfo (ActionExecutingContext filtercontext) {#regionGet the characters in a link//Get domain name if(FilterContext.HttpContext.Request.Url! =NULL) DomainName=filterContext.HttpContext.Request.Url.Authority; //Get module name//module = filtercontext.httpcontext.request.url.segments[1]. Replace ('/', '). Trim (); //Get controllername nameControllername = filtercontext.routedata.values["Controller"]. ToString (); //get the action nameActionName = filtercontext.routedata.values["Action"]. ToString (); #endregion } /// <summary> ///Get domain name/// </summary> Public stringDomainName {Get;Set; } /// <summary> ///Get module name/// </summary> Public stringModule {Get;Set; } /// <summary> ///Get controllername name/// </summary> Public stringcontrollername {Get;Set; } /// <summary> ///get the action name/// </summary> Public stringActionName {Get;Set; } } }}
I used the OnActionExecuting override method, which means that the filter is called before executing the method
Determine if the user is logged in, if not logged in, jump directly to the login page
is not very simple, do not know how security, I am also the first time to use
Welcome to the Great God.
ASP. NET MVC: 86594082 Welcome to your arrival.
MVC Filter Build member is logged in