- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 web api,其中全局配置被配置为使用:XmlMediaTypeFormatter
我的问题是我不会用一个新的 Controller 扩展这个 web api,而是使用 JsonMediaTypeFormatter。
是否可以仅将一个 API Controller 类的 MediaTypeFormatter 更改为 JSON?
我的问题不是返回 JSON,我已经通过返回 HttpResponseMessage 完成了这个:
return new HttpResponseMessage
{
Content = new ObjectContent<string>("Hello world", new JsonMediaTypeFormatter()),
StatusCode = HttpStatusCode.OK
};
这是我遇到问题的请求。如果我有一个具有两个属性的对象:
public class VMRegistrant
{
public int MerchantId { get; set; }
public string Email { get; set; }
}
我的 Controller 操作将 VMRegistrant 作为参数:
public HttpResponseMessage CreateRegistrant(VMRegistrant registrant)
{
// Save registrant in db...
}
但问题是当我使用 JSON 调用操作时它失败了。
最佳答案
你可以让你的 Controller 返回一个 IHttpActionResult
并使用扩展方法 HttpRequestMessageExtensions.CreateResponse<T>
并指定您要使用的格式化程序:
public IHttpActionResult Foo()
{
var bar = new Bar { Message = "Hello" };
return Request.CreateResponse(HttpStatusCode.OK, bar, new MediaTypeHeaderValue("application/json"));
}
另一种可能性是使用 ApiController.Content
方法:
public IHttpActionResult Foo()
{
var bar = new Bar { Message = "Hello" };
return Content(HttpStatusCode.OK, bar, new JsonMediaTypeFormatter(), new MediaTypeHeaderValue("application/json"));
}
编辑:
一种可能是自己从 Request
中读取和反序列化内容通过从流中读取对象并使用 JSON 解析器(例如 Json.NET)从 JSON 创建对象:
public async Task<IHttpActionResult> FooAsync()
{
var json = await Request.Content.ReadAsStringAsync();
var content = JsonConvert.DeserializeObject<VMRegistrant>(json);
}
关于c# - 是否可以仅将一个类的 MediaTypeFormatter 更改为 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33168590/
我已经编写了一个 ServiceHelper 类,它将协助 POST 到 C# Web API Controller public class ServiceHelper : IServiceHelp
我正在尝试根据请求的 URL 输出不同格式的类型。在 Preview5 之前,我执行了以下操作以在 MediaTypeFormatters OnWriteToStream-Method 中获取 URI
我有一个 web api,其中全局配置被配置为使用:XmlMediaTypeFormatter 我的问题是我不会用一个新的 Controller 扩展这个 web api,而是使用 JsonMedia
我有 extended the JsonMediaTypeFormatter 为了在 JSON 中为 decorated with a custom attribute 的类型生成“根”对象. 我将如
我在服务器上部署了一个 webapi 项目 mvc4 rc 和一个 mvc4 rc web 应用程序。 在本地一切正常,但在服务器上出现错误 2012-06-09 03:18:18,659 [23]
我有一个带有自定义 MediaTypeFormatter 的自托管 WebApi 应用程序 根据“名称”参数(或 URL 的一部分),应用程序应将请求正文格式化为不同的类型。 这是 Action //
AJAX 调用 $.ajax({ url: '/api/Inventory', cache: false, type: 'POST', data: json,
考虑这段代码: public Gender Get() { return Gender.Female; } public enum Gender { Male, Female }
我在 TestFormatter.cs extending MediaTypeFormatter 中有一个名为 TestFormatter 的自定义媒体格式化程序。现在我想添加这个格式化程序。一种选择
我有一个 MVC 5 Web Api 和一个连接到它的桌面应用程序。 我有一个 Controller 并从应用程序连接到它没有任何问题,但是当我将 [Authorize] 属性放入 Controlle
我有一个非常简单的 ApiController,它在 Win7 上运行良好,但在 Windows 2003 Server 上我遇到错误。 获取请求(来自浏览器或 $.getJson): https:/
我正在实现我自己的 MediaTypeFormatter我对方法有一些疑问 WriteToStreamAsync对于我正在做的自定义 XML 序列化程序: public override Task W
我创建了一个 WebAPI 项目来帮助捕获 TinCan 学习类(class)中的语句,但我在检索任何请求负载详细信息时遇到了极大的困难。在此有效负载中,我传递了我试图捕获的整个语句,但在尝试使用以下
我正在尝试自定义实现 MediaTypeFormatter , 但方法 bool CanReadType(Type type)总是以 IKeyValueModel 类型调用,而不是我的类型 MyPro
考虑使用 ASP.NET Web API 编写的 Web 服务来接受任意数量的文件作为“多部分/混合”请求。辅助方法如下所示(假设 _client 是 System.Net.Http.HttpClie
我正在使用 ASP.NET Web API。我的 Controller 中有一个 Action ,如果没有参数,它可以正常工作。如果有一个参数,例如: public string UploadFile
我正在为 HTML 编写媒体类型格式化程序,以根据用户的 html 请求自动生成 Razor View 。我这样做是为了在 SelfHosted 服务中使用。我需要检测请求了哪些 Controller
当我尝试使用 ASP.NET WEBAPI 将我的表单数据发布到 JSON 文件时,我收到“没有 MediaTypeFormatter 可用于从媒体类型为‘text/plain’的内容中读取类型为‘P
我有一个 ASP.Net WebAPI 项目将返回 JSON,但特别是 HAL+JSON。我知道我可以编写自定义 MediaTypeFormatter 来执行此操作,但我计划返回相同的 HAL 格式响
情况是这样的: 它们是 Servoy 中的外部 Web 服务,我想在 ASP.NET MVC 应用程序中使用此服务。 通过这段代码,我尝试从服务中获取数据: HttpResponseMessage r
我是一名优秀的程序员,十分优秀!