2016-11-29 12 views
1

クライアントからのパラメータでweb api 2カスタム属性を作成し、クライアントにデータを返すときに問題があります。クライアントからのサンプルデータのためのweb api 2カスタム属性を作成してクライアントに返す方法は?

{ id : 1, _token : 221 } 

例えば、サーバー上:

//SomeController Action 
[CustomActionFilter] 
public String Foo(int id) 
{ 
    return id; 
} 

public class CustomActionFilter : ActionFilterAttribute, IActionFilter 
{ 
    void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     //get _token from client httprequest parameter 
     //return to client if _token != 221 
     //next proccess if _token == 221 
     // in real case, will cek token in db and compare it 
    } 
} 

CustomActionFilter_tokenを取得し、_token場合はJSON形式でクライアントのエラーメッセージに戻す方法を助けてください=! 221?

また、問題がなければ、次のプロセスを実行しますか?

答えて

1

多くのことが間違っています。まず、ActionFilterAttribute自体がIActionFilterを実装しているため、このインターフェイスをカスタム属性に再度実装する必要はありません。次に、トークンはFoo関数に渡されないため、基本的にFoo関数の仮パラメータを変更する必要があります。しかし、あなたはこれを行うにはどのようにアイデアを与えるために、これをチェック:

var response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Forbidden, "my reasonable error message"); 
actionContext.Response = response; 

public class CustomActionFilter : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(HttpActionContext actionContext) 
    { 
     var id = actionContext.ActionArguments["id"]; 
    } 
} 

を最後に、エラーを返すために、あなたはOnActionExecutingメソッド内でこの権利を行うことができます

関連する問題