2

属性で装飾されたプロパティを別の名前のリクエスト プロパティにバインドできるカスタム モデル バインダーを作成しようとしています。

JSON リクエスト

{
    "app": "acme"
}

依頼モデル(抜粋)

[Alias("app")]
public string ApplicationName { get; set; }

...ApplicationName値「acme」が入力されるはずです。このためのカスタム モデル バインダーの作成に行き詰まっています。

モデルバインダー

public BindToAliasModelBinder : IModelBinder {
    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) {
        ...
    }
}

モデル バインダー プロバイダー

public class BindFromAliasModelBinderProvider : ModelBinderProvider {
    public override IModelBinder GetBinder(HttpConfiguration configuration, Type modelType) {
        return new BindFromAliasModelBinder();
    }
}

プロバイダーをグローバルに登録しましたが、バインダーは期待どおりにヒットしています。次に何をすべきか途方に暮れています - リクエスト値を繰り返し処理し、属性の存在に基づいて条件付きでバインドするにはどうすればよいですか?

4

1 に答える 1

1

エイリアシングだけを行いたい場合は、プロパティのJsonPropertyAttributeようなものを使用でき[JsonProperty(PropertyName = "app")]ます。

于 2013-03-11T21:27:52.807 に答える