使用C#网页API与其他内容类型 [英] Using c# web api with alternate content-type

查看:134
本文介绍了使用C#网页API与其他内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的网页API,我需要创建一个客户端的服务器。我有超过客户端无法控制 - 不能改变的事情

I'm new to web api and I need to create a server for a client. I have no control over the client - can't change a thing.

客户端在一个POST体内的HTML封装JSON请求发送。然而,内容类型可以变化。什么我需要做的,让我ApiController处理不同内容类型?

The client sends in an html encapsulated json request in a POST body. However, the content-type can vary. What do I need to do to allow my ApiController to process different content-types?

推荐答案

引擎盖下的Web API支持的内容协商的机制基础上,头会自动选择正确的格式内容类型在HTTP请求。​​

Under the hood, Web Api supports Content Negotiation mechanism to automatically opt the correct formatter based on the header Content-Type in HTTP request.

在默认情况下内容协商支持三种格式化: JSON XML 表单urlen codeD数据。如果没有发现任何格式,客户端将收到HTTP错误406(不能接受)。

By default content negotiation supports three formatters: json, xml and form-urlencoded data. If no formatter found, client will receives HTTP error 406 (Not Acceptable).

查看更多:

<一个href=\"http://www.asp.net/web-api/overview/formats-and-model-binding/content-negotiation\">http://www.asp.net/web-api/overview/formats-and-model-binding/content-negotiation

如果你需要允许Web API支持另一个内容类型,您可以编写自己的自定义格式:

If you need to allow Web Api support another Content-Type, you can write your own custom formatter:

<一个href=\"http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters\">http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters

这篇关于使用C#网页API与其他内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆