相关疑难解决方法(0)

转换自定义操作筛选器以供Web API使用?

我发现了一个非常好的动作过滤器,它将逗号分隔的参数转换为泛型类型列表:http://stevescodingblog.co.uk/fun-with-action-filters/

我想使用它,但它不适用于ApiController,它完全忽略它.有人可以帮助转换它以供Web API使用吗?

[AttributeUsage(AttributeTargets.Method)]
public class SplitStringAttribute : ActionFilterAttribute
{
    public string Parameter { get; set; }

    public string Delimiter { get; set; }

    public SplitStringAttribute()
    {
        Delimiter = ",";
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.ActionParameters.ContainsKey(this.Parameter))
        {
            string value = null;
            var request = filterContext.RequestContext.HttpContext.Request;

            if (filterContext.RouteData.Values.ContainsKey(this.Parameter)
                && filterContext.RouteData.Values[this.Parameter] is string)
            {
                value = (string)filterContext.RouteData.Values[this.Parameter];
            }
            else if (request[this.Parameter] is string)
            {
                value = request[this.Parameter] as string;
            }

            var listArgType = GetParameterEnumerableType(filterContext);

            if (listArgType …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc action-filter c#-4.0 asp.net-web-api

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

标签 统计

action-filter ×1

asp.net-mvc ×1

asp.net-web-api ×1

c# ×1

c#-4.0 ×1