Action Filter

代码
using  System.Web;
using  System.Web.Mvc;
using  System.IO;

namespace  MvcApp1.ActionFilters
{
    
public   class  SimpleActionFilterAttribute : ActionFilterAttribute
    {
        
public   override   void  OnActionExecuting(ActionExecutingContext filterContext)
        {
            File.AppendAllText(
@" f:\ActinoFilter.log " , ID  +   "  : OnActionExecuting "   +  Environment.NewLine );
        }
        
public   override   void  OnActionExecuted(ActionExecutedContext filterContext)
        {
            File.AppendAllText(
@" f:\ActinoFilter.log " , ID  +   "  : OnActionExecuted "   +  Environment.NewLine);
        }
        
public   override   void  OnResultExecuting(ResultExecutingContext filterContext)
        {
            File.AppendAllText(
@" f:\ActinoFilter.log " , ID  +   "  : OnResultExecuting "   +  Environment.NewLine);
        }
        
public   override   void  OnResultExecuted(ResultExecutedContext filterContext)
        {
            File.AppendAllText(
@" f:\ActinoFilter.log " , ID  +   "  : OnResultExecuted "   +  Environment.NewLine);
        }
        
public   int  ID {  get set ; }
    }
////////////////////////////// //

using  System.Web;
using  System.Web.Mvc;

namespace  MvcApp1.ActionFilters
{
    
public   class  AuthenticationActionFilterAttribute:ActionFilterAttribute
    {
        
public   override   void  OnActionExecuting(ActionExecutingContext filterContext)
        {
            
if  ( ! filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                filterContext.Result 
=   new  RedirectToRouteResult( " Default " new  System.Web.Routing.RouteValueDictionary( new  { action = " SignIn " }));
                
            }
        }
    }
}

/////////////////////////////////////////////////// /

using  System.Web;
using  System.Web.Mvc;
using  System.IO.Compression;

namespace  MvcApp1.ActionFilters
{
    
public   class  CompressActionFilterAttribute:ActionFilterAttribute
    {
        
public   override   void  OnResultExecuting(ResultExecutingContext filterContext)
        {
            
string  acceptEncoding  =  filterContext.HttpContext.Request.Headers[ " Accept-Encoding " ].ToLower();
            
if  ( string .IsNullOrEmpty(acceptEncoding))  return ;

            var response 
=  filterContext.HttpContext.Response;
            
if  (acceptEncoding.Contains( " gzip " ))         // , deflate
            {
                response.AddHeader(
" Content-Encoding " " gzip " );
                response.Filter 
=   new  GZipStream(response.Filter, CompressionMode.Compress);
            }
            
else    if  (acceptEncoding.Contains( " deflate " ))         // , deflate
            {
                response.AddHeader(
" Content-Encoding " " deflate " );
                response.Filter 
=   new  DeflateStream(response.Filter, CompressionMode.Compress);
            }
        }
    }

 

你可能感兴趣的:(filter)