1

以前、WebAPIコントローラーを次のコードで正しく動作させていました。

    [Queryable(ResultLimit = 30)]
    public IQueryable<Lead> Get()
    {
        return _db.Leads;
    }

次に、Leadsテーブルに外部キーを追加しました。JSONループエラーが発生していました。そこで、コントローラーから返す必要のあるフィールドのみを選択することにしました。コードの例を次に示します。

    [Queryable(ResultLimit = 30)]
    public IQueryable<dynamic> Get()
    {
        return _db.Leads.Select(x => new
                                    {
                                        x.FirstName,
                                        x.LeadTypeID,
                                        x.DateSent
                                    }).AsQueryable();
    }

アプリケーションまたはターミナルからAPIを呼び出すと、結果または正しく返されます。ただし、ブラウザ(Chrome)にURLを貼り付けると、サーバー500エラーが発生します。JSONエラーではなくXMLを推測します。関係を追加する前に機能しました。前の問題を解決したいのですが、それは重要ではありません。懸念される領域は、データが返されることですが、私のODataフィルターは無視されています。

あなたの助けは大歓迎です。

4

1 に答える 1

1

The built in XML serializer doesn't support anonymous types, which is why you're getting a runtime exception. There are 3 ways you can potentially solve this:

  1. Create a custom POCO to return instead of your anonymous type.
  2. You can remove the XML serializer and just have your application always return JSON. You can disable the serializer by adding: GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); to your Application_Start() method of your global.asax.cs file.
  3. You can choose a different XML Serializer that supports anonymous types. I don't know of any off hand, but I'm sure there's a few out there.
于 2012-10-03T19:15:36.250 に答える