5

そのため、何人かの人々 (ここここなど) がApiControllers の MVC4 モデル バインディングで問題を抱えているようですが、私が見ている問題に完全に対処しているようには見えません。

私が本当にやりたいのは、整数のリストに対する配列バインディングの動作を変更することだけです。たとえば、次のようなリクエスト タイプがあるとします。

public class MyRequestModel
{
    public List<long> ListOfIntegers { get; set; }

    ...
}

そして、次のような API GET メソッド:

public ResultsResponseModel Get(MyRequestModel request)
{
    // use request.ListOfIntegers meaningfully

    ...

    return response;
}

基本的にはその物件に言えるようになりたいし/api/results/?listOfIntegers=1+2+3+4+5、その決意を持ってもらいたいです。List<long>

私は通常のモデル バインディングのトリックを試しましたが、MVC4 のほとんどの Web API と同様に、モデル バインディングにはまったく別のパスがあるようです。

私が得た最も遠いものは、 のSystem.Web.Http.ModelBinding.ModelBinder属性を使用しMyRequestModel、 を「実装」したモデル バインダーを作成することSystem.Web.Http.ModelBinding.IModelBinderです。これにより、コードに触れることのないスタック トレースで常にオブジェクト参照例外が発生します。

これ打った人いる?次に何をしようか考えていますか?

更新: カスタムでキャプチャしたスタック トレースは次のExceptionFilterAttributeとおりです。

Object reference not set to an instance of an object.
    at System.Web.Http.ModelBinding.DefaultActionValueBinder.BindParameterValue(HttpActionContext actionContext, HttpParameterBinding parameterBinding)
    at System.Web.Http.ModelBinding.DefaultActionValueBinder.<>c__DisplayClass1.BindValuesAsync>b__0(RequestContentReadKind contentReadKind)
    at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass38.<ToAsyncVoidTask>b__37()
    at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
4

1 に答える 1

4

ApiControllersについて話している場合は、Web APIでモデルバインドを試みており、MVCはサンプルモデルバインダーです。

  public class MyRequestModelBinderProvider : ModelBinderProvider
    {
        MyRequestModelBinder binder = new MyRequestModelBinder();
        public IdeaModelBinderProvider()
        {          
        }

        public override IModelBinder GetBinder(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType == typeof(MyRequestModel))
            {
                return binder;
            }

            return null;
        }
    } 

カスタムモデルバインダープロバイダーを登録する例を次に示します。

 IEnumerable<object> modelBinderProviderServices = GlobalConfiguration.Configuration.ServiceResolver.GetServices(typeof(ModelBinderProvider));
 List<Object> services = new List<object>(modelBinderProviderServices);
 services.Add(new MyRequestModelBinderProvider());
 GlobalConfiguration.Configuration.ServiceResolver.SetServices(typeof(ModelBinderProvider), services.ToArray());

これで、カスタムモデルバインダーで、コンテキストを使用してクエリ文字列値にアクセスします

  public class MyRequestModelBinder :  IModelBinder
    {
        public MyRequestModelBinder()
        {

        }

        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            MyRequestModel yourModel; 
            //use contexts to access query string values
            //create / update your model properties

            bindingContext.Model = yourModel;  
            //return true || false if binding is successful
        }

MVCではなくWebAPIのクラスとインターフェイスを使用していることを確認してください。一部の名前は同じですが、名前空間とdllが異なります

于 2012-04-11T03:48:01.193 に答える