MVC6的Web API - 返回纯文本 [英] MVC6 Web Api - Return Plain Text

查看:286
本文介绍了MVC6的Web API - 返回纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过其他类似的问题(比如这个<一个href=\"https://stackoverflow.com/questions/11581697/is-there-a-way-to-force-asp-net-web-api-to-return-plain-text\">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 - 返回纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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