43

入力に基づいて出力 XML または JSON データを取得しようとしています。以下の WEB API コードを使用しましたが、正確な出力ができませんでした。

public string Get(int id)
{
    if (GlobalConfiguration.Configuration.Formatters.XmlFormatter == null)
    {
        GlobalConfiguration.Configuration.Formatters.Add(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
    }
    if (GlobalConfiguration.Configuration.Formatters.JsonFormatter == null)
    {
        GlobalConfiguration.Configuration.Formatters.Add(GlobalConfiguration.Configuration.Formatters.JsonFormatter);
    }
    if (id == 1)
    {
        GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.JsonFormatter);                
        GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;                
    }
    else
    {
        GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.UseDataContractJsonSerializer = true;
    }
    return "value";
}
4

7 に答える 7

81

app_start以下のコードイベントをファイルに追加しglobal.asaxます。API Url にクエリ文字列を追加します。

GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
    new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));

GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(
    new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));

例えば:

for xml : http://localhost:49533/api/?type=xml

for json: http://localhost:49533/api/?type=json
于 2013-11-14T13:31:36.393 に答える
8

これをもう少し調べて、別の投稿で答えを見つけました:

public HttpResponseMessage Get(int id)
{
    string content = "value";

    if (id == 1)
    {
        return Request.CreateResponse<string>(HttpStatusCode.OK, content, Configuration.Formatters.JsonFormatter);
    }

    return Request.CreateResponse<string>(HttpStatusCode.OK, content, Configuration.Formatters.XmlFormatter);
}
于 2014-10-23T08:30:57.780 に答える
6

また、受け入れヘッダーを強制するためにも機能します。いつも戻ってくるとは限らない場合に最適なオプションですHttpResponseMessage's。いえ

Request.Headers.Add("Accept", "text/json");
return Request.CreateResponse(HttpStatusCode.OK, yourobject);

また

Request.Headers.Add("Accept", "application/xml");
return new Rss20FeedFormatter(feed);
于 2015-09-29T19:52:08.993 に答える
5

リクエストで MIME タイプを指定するapplication/jsonと、Web API はレスポンスを適切にフォーマットします。

Web API を手動でデバッグしようとしている場合は、Fiddler 2などのツールを使用してタイプを指定します。

この記事では、概念について説明します。

于 2013-11-14T13:42:19.280 に答える
3

vijayjan15によって受け入れられた回答は、特定の状況 (つまり、MediaTypeMappings を使用する) に最適な方法のようですが、代わりに、XML を返すメソッドと JSON を返すメソッドの 2 つの異なるメソッドを使用することもできます。そのために、コントローラー固有の HttpConfiguration をインスタンス化できます (GlobalConfiguration.Configuration の変更を避けるため)。

public MyReturnType GetMyTypeAsXml() {
    Configuration = new HttpConfiguration();
    Configuration.Formatters.Clear();
    Configuration.Formatters.Add(new XmlMediaTypeFormatter());

    return new MyReturnType();
}

public MyReturnType GetMyTypeAsJson() {
    Configuration = new HttpConfiguration();
    Configuration.Formatters.Clear();
    Configuration.Formatters.Add(new JsonMediaTypeFormatter());

    return new MyReturnType();
}

HttpConfiguration の新しいインスタンスをスピンアップする際にどれだけのオーバーヘッドが発生するかはわかりませんが (あまり多くないと思います)、新しいインスタンスにはデフォルトで Formatters コレクションが含まれているため、インスタンス化した直後にそれをクリアする必要があります。それ。Configuration = new HttpConfiguration() を使用せず、代わりに Configuration を直接変更すると、GlobalConfiguration.Configuration プロパティが変更されることに注意してください(そのため、他のすべての WebApi メソッドに影響を与えます - 悪い!)。

于 2014-07-25T23:32:35.463 に答える