我需要从ASP.NET Web API控制器以纯文本形式获得响应.
我试过做一个请求,Accept: text/plain但它似乎没有做的伎俩.此外,请求是外部的,不受我的控制.我要做的是模仿旧的ASP.NET方式:
context.Response.ContentType = "text/plain";
context.Response.Write("some text);
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
编辑,解决方案:根据Aliostad的回答,我添加了WebAPIContrib文本格式化程序,在Application_Start中初始化它:
config.Formatters.Add(new PlainTextFormatter());
Run Code Online (Sandbox Code Playgroud)
我的控制器最终结果如下:
[HttpGet, HttpPost]
public HttpResponseMessage GetPlainText()
{
return ControllerContext.Request.CreateResponse(HttpStatusCode.OK, "Test data", "text/plain");
}
Run Code Online (Sandbox Code Playgroud)