如何使用ApiController返回原始string?

我有一个ApiController服务XML / JSON,但我想我的行动之一,以返回纯HTML。 我试过下面,但它仍然返回XML / JSON。

public string Get() { return "<strong>test</strong>"; } 

这就是上面的回报:

 <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">&lt;strong&gt;test&lt;/strong&gt;</string> 

有没有办法只返回纯粹的,未转义的文本,甚至没有周围的XML标签(也许是不同的返回types的行为属性)?

你可以让你的Web Api操作返回一个HttpResponseMessage ,你可以完全控制内容。 在你的情况下,你可能会使用一个StringContent并指定正确的内容types:

 public HttpResponseMessage Get() { return new HttpResponseMessage() { Content = new StringContent( "<strong>test</strong>", Encoding.UTF8, "text/html" ) }; } 

只要return Ok(value)将不起作用,它将被视为IEnumerable<char>

而是使用return Ok(new { Value = value })或者simillar。

另一个可能的解 在Web API 2中,我使用了APIController的base.Content()方法:

  public IHttpActionResult Post() { return base.Content(HttpStatusCode.OK, new {} , new JsonMediaTypeFormatter(), "text/plain"); } 

我需要这样做来解决IE9的问题,它一直试图下载JSON内容。 这也应该适用于XMLtypes的数据通过使用XmlMediaTypeFormatter媒体格式化程序。

希望能帮助别人。

我们必须努力不要从我们的API中返回HTML而是纯数据,并在UI中相应地格式化数据,但是也许可以使用:

 return this.Request.CreateResponse(HttpStatusCode.OK, new{content=YourStringContent}) 

这个对我有用

我从mvc控制器方法中调用以下webapi2控制器方法:

 <HttpPost> Public Function TestApiCall(<FromBody> screenerRequest As JsonBaseContainer) As IHttpActionResult Dim response = Me.Request.CreateResponse(HttpStatusCode.OK) response.Content = New StringContent("{""foo"":""bar""}", Encoding.UTF8, "text/plain") Return ResponseMessage(response) End Function 

我从asp.net服务器上的这个例程调用它:

 Public Async Function PostJsonContent(baseUri As String, requestUri As String, content As String, Optional timeout As Integer = 15, Optional failedResponse As String = "", Optional ignoreSslCertErrors As Boolean = False) As Task(Of String) Return Await PostJsonContent(baseUri, requestUri, New StringContent(content, Encoding.UTF8, "application/json"), timeout, failedResponse, ignoreSslCertErrors) End Function Public Async Function PostJsonContent(baseUri As String, requestUri As String, content As HttpContent, Optional timeout As Integer = 15, Optional failedResponse As String = "", Optional ignoreSslCertErrors As Boolean = False) As Task(Of String) Dim httpResponse As HttpResponseMessage Using handler = New WebRequestHandler If ignoreSslCertErrors Then handler.ServerCertificateValidationCallback = New Security.RemoteCertificateValidationCallback(Function(sender, cert, chain, policyErrors) True) End If Using client = New HttpClient(handler) If Not String.IsNullOrWhiteSpace(baseUri) Then client.BaseAddress = New Uri(baseUri) End If client.DefaultRequestHeaders.Accept.Clear() client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json")) client.Timeout = New TimeSpan(TimeSpan.FromSeconds(timeout).Ticks) httpResponse = Await client.PostAsync(requestUri, content) If httpResponse.IsSuccessStatusCode Then Dim response = Await httpResponse.Content.ReadAsStringAsync If Not String.IsNullOrWhiteSpace(response) Then Return response End If End If End Using End Using Return failedResponse End Function