小编Mat*_*att的帖子

ASP.NET MVC 3:您是否可以创建一个ActionFilter来访问请求参数,然后修改它们以适合Action Method签名

我正在尝试将一些安全性/验证日志记录添加到我正在处理的ASP.NET MVC 3应用程序中.我想做的是使用类似于此的Action过滤器来装饰我的一些Action方法:

public class RegexValidateAttribute : ActionFilterAttribute
{
    private static readonly ILog logger =
        LogManager.GetLogger( typeof( RegexValidateAttribute ).FullName );

    public string ParameterName { get; set; }
    public string Expression { get; set; }

    public override void OnActionExecuting( ActionExecutingContext filterContext )
    {
        string parameterValue = Convert.ToString(
            filterContext.ActionParameters[ ParameterName ] );
        if ( parameterValue != null )
        {
            if ( !Regex.IsMatch( parameterValue, Expression ) )
            {
                logger.Error( "INPUT_VALIDATION_EXCEPTION [ Parameter "
                              + ParameterName + " did not match the regex: '" …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc-3

2
推荐指数
1
解决办法
751
查看次数

标签 统计

asp.net-mvc-3 ×1