MVC6的Web API

编程入门 行业动态 更新时间:2024-10-12 01:22:55
本文介绍了MVC6的Web API - 返回纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经看过其他类似的问题(比如这个Is有没有办法强制的ASP.NET Web API返回纯文本?)对SO,但他们似乎都解决的WebAPI 1或2,而不是你MVC6使用最新版本。

I have looked at other similar questions (such as this one Is there a way to force ASP.NET Web API to return plain text?) on SO but they all seem to address WebAPI 1 or 2, not the latest version you use with MVC6.

我需要在我的Web API控制器之一返回纯文本。只有一个 - 其他应回头率JSON。该控制器可用于开发目的,产出数据库中的记录名单,该名单将被导入流量负载生成器。该工具将CSV作为输入,所以我想输出(用户只需要保存网页的内容)。

I need to return plain text on one of my Web API controllers. Only one - the others should keep returning JSON. This controller is used for dev purposes to output a list of records in a database, which will be imported into a traffic load generator. This tool takes CSV as input so I'm trying to output that (users will only have to save the content of the page).

[HttpGet] public HttpResponseMessage AllProductsCsv() { IList<Product> products = productService.GetAllProducts(); var sb = new StringBuilder(); sb.Append("Id,PartNumber"); foreach(var product in products) { sb.AppendFormat("{0},{1}", product.Id, product.PartNumber); } HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StringContent(sb.ToString(), System.Text.Encoding.UTF8, "text/plain"); return result; }

根据不同的搜索,这似乎是着手,因为我需要这个只有这一个动作最简单的方法。然而,当我请求,我得到以下输出:

Based on various searches this seems like the simplest way to proceed since I'll need this only for this one action. However when I request this, I get the following output:

{    "Version": {       "Major": 1,       "Minor": 1,       "Build": -1,       "Revision": -1,       "MajorRevision": -1,       "MinorRevision": -1    },    "Content": {       "Headers": [          {             "Key": "Content-Type",             "Value": [                "text/plain; charset=utf-8"             ]          }       ]    },    "StatusCode": 200,    "ReasonPhrase": "OK",    "Headers": [],    "RequestMessage": null,    "IsSuccessStatusCode": true }

如此看来,MVC仍试图输出JSON,我不知道为什么他们倒是输出这些值。当我调试一步的code一步,我可以看到,StringBuilder的内容是好的,什么我要输出。

So it seems that MVC still tries to output JSON, and I have no idea why they'd output these values. When I debug the code step by step I can see that the content of the StringBuilder is okay and what I want to output.

有没有简单的方法,只是输出MVC6字符串?

Is there any easy way to just output a string with MVC6?

推荐答案

解决方案是返回一个FileContentResult。这似乎是绕过内置格式化:

The solution was to return a FileContentResult. This seems to bypass the built-in formatters:

[HttpGet] public FileContentResult AllProductsCsv() { IList<Product> products = productService.GetAllProducts(); var sb = new StringBuilder(); sb.Append("Id,PartNumber\n"); foreach(var product in products) { sb.AppendFormat("{0},{1}\n", product.Id, product.PartNumber); } return File(Encoding.UTF8.GetBytes(sb.ToString()), "text/csv"); }

更多推荐

MVC6的Web API

本文发布于:2023-10-12 04:35:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483730.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Web   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!