How to return raw string with ApiController?

前端 未结 6 883
萌比男神i
萌比男神i 2020-11-29 21:49

I have an ApiController that serves XML/JSON, but I would like one of my actions to return pure HTML. I tried the below but it still return XML/JSON.

public          


        
6条回答
  •  青春惊慌失措
    2020-11-29 22:38

    You could have your Web Api action return an HttpResponseMessage for which you have full control over the Content. In your case you might use a StringContent and specify the correct content type:

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

    or

    public IHttpActionResult Get()
    {
        return base.ResponseMessage(new HttpResponseMessage()
        {
            Content = new StringContent(
                "test", 
                Encoding.UTF8, 
                "text/html"
            )
        });
    }
    

提交回复
热议问题